In object-oriented programming (OOP), the concept of inheritance helps us represent the "is a" relationship between objects. This means that one object is a specialized version of another object. Let's look at some real-world examples:
- A car is a vehicle.
- A rose is a flower.
- A circle is a shape.
- A tree is a plant.
When an "is a" relationship exists, the specialized object (like a rose) has all the characteristics of the general object (like a flower) plus some additional characteristics that make it special.
Example:
Let’s create a Car superclass and two subclasses: RV and Truck. The Car class will have common attributes, while RV and Truck will have additional unique attributes.
Superclass: Car
- Attributes: make, model, and year are common to all cars.
- Method: print_info() prints out information about the car.
Subclass 1: RV
- Inheritance: By using class RV(Car):, we declare that RV is a subclass of Car.
- Calling the Superclass Constructor: super().__init__(make, model, year) initializes the common attributes.
- New Attribute: rv_type is a new attribute unique to RV.
- Overriding a Method: print_info() is redefined in RV to include rv_type.
Subclass 2: Truck
- Inheritance: By using class Truck(Car):, we declare that Truck is a subclass of Car.
- Calling the Superclass Constructor: super().__init__(make, model, year) initializes the common attributes.
- New Attribute: capacity is a new attribute unique to Truck.
- Overriding a Method: print_info() is redefined in Truck to include capacity.
Creating Objects and Using Inherited Methods
Let’s create objects of RV and Truck and see how inheritance works:
- Creating and Using an RV Object
- Creating and Using a Truck Object
Key Points
- Superclass and Subclass: Car is the superclass, and RV and Truck are subclasses.
- Inheritance: Both RV and Truck inherit attributes and methods from Car.
- Constructor Call: super().__init__(make, model, year) ensures that the Car constructor is called.
- Method Overriding: The print_info() method in both RV and Truck overrides the method in Car.