What is Inheritance?

14
0

Inheritance is a principle of object-oriented programming that allows a class to inherit characteristics from a parent class. This can save time and improve code reuse.

Here is an example of inheritance in Python:

class Animal:
    def __init__(self, name, species):
        self.name = name
        self.species = species
        
    def make_sound(self):
        pass
    

class Dog(Animal):
    def __init__(self, name, breed):
        super().__init__(name, species="Dog")
        self.breed = breed
        
    def make_sound(self):
        return "Bark"
    

class Cat(Animal):
    def __init__(self, name, breed):
        super().__init__(name, species="Cat")
        self.breed = breed
        
    def make_sound(self):
        return "Meow"

In this example, the Animal class is the parent class, and the Dog and Cat classes are child classes that inherit from the Animal class. The child classes inherit the name and species attributes, as well as the make_sound method, from the parent class. They also have their own attributes (breed) and methods (__init__).

The super().__init__(name, species="Dog") line calls the __init__ method of the parent class, passing it the name argument and setting the species argument to “Dog” for the Dog class or “Cat” for the Cat class. This allows the child classes to reuse the code in the parent class’s __init__ method and customize it for their own needs.

Using inheritance, you can create a hierarchy of classes, where child classes inherit characteristics from their parent class and can also have their own unique characteristics. This can help to organize and structure your code in a logical way.

Here are a few more examples of inheritance in Python:

class Vehicle:
    def __init__(self, make, model):
        self.make = make
        self.model = model
        
    def start_engine(self):
        return "Engine started"
    

class Car(Vehicle):
    def __init__(self, make, model, num_wheels):
        super().__init__(make, model)
        self.num_wheels = num_wheels
        

class Motorcycle(Vehicle):
    def __init__(self, make, model, num_wheels):
        super().__init__(make, model)
        self.num_wheels = num_wheels
        

class Truck(Vehicle):
    def __init__(self, make, model, num_wheels, cargo_capacity):
        super().__init__(make, model)
        self.num_wheels = num_wheels
        self.cargo_capacity = cargo_capacity
        

class Boat(Vehicle):
    def __init__(self, make, model, num_propellers):
        super().__init__(make, model)
        self.num_propellers = num_propellers
        
    def start_engine(self):
        return "Engine started. Remember to untie the dock lines."

In this example, the Vehicle class is the parent class, and the Car, Motorcycle, Truck, and Boat classes are child classes that inherit from the Vehicle class. They all inherit the make and model attributes, as well as the start_engine method, from the parent class. They also have their own unique attributes (e.g., num_wheels, num_propellers, cargo_capacity).

The Boat class has a start_engine method that overrides the one in the parent class. This means that when the start_engine method is called on an instance of the Boat class, it will execute the code in the Boat class’s start_engine method instead of the one in the parent class. This is an example of polymorphism, which allows you to use the same method name to perform different actions depending on the context.

Leave a Reply

Your email address will not be published. Required fields are marked *