Cards (7)

  • What is Inheritance? #1
    • Inheritance is a key concept in object-oriented programming (OOP) that allows a class to inherit the properties and behaviours (methods and attributes) of another class 
    • Inheritance promotes code reuse by allowing derived classes to inherit and utilise the existing code from the base class. This avoids duplicating code and promotes better organization and maintainability 
    • Inheritance establishes an "IS-A" relationship between the base class and the derived class 
  • Inheritance #2
    • For example, if you have a base class called Vehicle and a derived class called Car, you can say that "a Car is a Vehicle." 
    • The car class inherits the properties and behaviours associated with being a vehicle 
    • Inheritance involves two main entities: 
    • The base class (also known as the parent class or superclass) and the derived class (also known as the child class or subclass
    • The derived class inherits the characteristics of the base class, meaning it can access and use the methods and attributes defined in the base class
  • Inheritance Image
  • Inheritance #3
    • Base Class: The base class serves as the blueprint or template from which the derived class inherits  
    • It defines common properties and behaviours that can be shared among multiple derived classes 
    • Derived Class: The derived class inherits the attributes and methods of the base class 
    • It can add additional attributes and methods 
  • Inheritance #3
    • If a car object was to be created, it may have the following attributes
    • Manufacturer - The company that makes the car 
    • Make -The model of the car 
    • Cost – The price of the car to purchase 
    • IsInsured – Whether or not the car is insured 
    • EngineCapacity – The size of the engine for the car 
  • Inheritance #4
    • It may also have access to the following methods: 
    • TurnEngineOn() – To start the car engine 
    • TurnEngineOff() – To turn off the car engine 
    • SteerLeft() – To turn the car to the left 
    • SteerRight() – To steer the car to the left 
    • GearChange() – To change the gear of the car 
    • UnlockDoors() – To unlock the doors to the car 
  • Inheritance #5
    • The above methods are only a select few and there could be many more added for extra functionality 
    • In the following code, the super keyword is used in inheritance to refer to the superclass (Base class: Vehicles) and access its members (methods, attributes, or constructors) from within the subclass (Derived Class: Cars)