Sitemap
Akheelmohammad

Akheelmohammad

Machine Learning enthusiast

Inheritance in oops-python

6 min readJan 9, 2023

--

Inheritance is a way to create a new class that is a modified version of an existing class, without having to modify the existing class. The new class is called the subclass, and the existing class is the superclass. The subclass inherits attributes and behaviors from the superclass, but it can also have its own attributes and behaviors that are specific to the subclass.

or

The process of inheriting the properties of the parent class into a child class is called inheritance. The existing class is called a base class or parent class and the new class is called a subclass or child class or derived class.

Inheritance is a powerful tool in object-oriented programming and can help you design reusable and maintainable code.

Here is an example that might help clarify things:

class Car:
def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year
def honk(self):
print("Honk honk!")
class ElectricCar(Car):
def __init__(self, make, model, year):
super().__init__(make, model, year) # Call the superclass's __init__ method
self.battery_size = 75
def honk(self):
print("Beep beep!")

my_car = ElectricCar("Tesla", "Model S", 2019)
print(my_car.make) # Output: "Tesla"
print(my_car.model) # Output: "Model S"
print(my_car.year) # Output: 2019
print(my_car.battery_size) # Output: 75
my_car.honk() # Output: "Beep beep!"

In this example, the ElectricCar class is a subclass of the Car class. It inherits the __init__ and honk methods from the Car class, but it also has its own battery_size attribute. When you create an instance of the ElectricCar class and call the honk method, it will use the version of the honk method that is defined in the ElectricCar class, rather than the version defined in the Car class.

There are several types of inheritance that you can use in Python:

Single inheritance:

Single inheritance is the most basic type of inheritance, where a subclass inherits attributes and behaviors from a single superclass. This is implemented by specifying the superclass in the parentheses following the name of the subclass.

Here is an example of single inheritance in Python:

class Animal:
def __init__(self, name, species):
self.name = name
self.species = species
def make_sound(self, sound):
print(f"This animal makes {sound}")

class Cat(Animal):
def __init__(self, name, breed, toy):
super().__init__(name, species="Cat") # Call super class's __init__ method
self.breed = breed
self.toy = toy
def play(self):
print(f"{self.name} plays with {self.toy}")

In this example, the Cat class is a subclass of the Animal class, which means it inherits the __init__ and make_sound methods from the Animal class. The Cat class can also have its own attributes and behaviors, such as the breed and toy attributes and the play method.

When you create an instance of the Cat class, it will have all of the attributes and behaviors of the Animal class, as well as its own attributes and behaviors.

c = Cat("Fluffy", "Siamese", "String")
print(c.name) # Output: "Fluffy"
print(c.species) # Output: "Cat"
print(c.breed) # Output: "Siamese"
c.make_sound("Meow") # Output: "This animal makes Meow"
c.play() # Output: "Fluffy plays with String"

Multiple inheritance:

Multiple inheritance is a type of inheritance where a subclass can inherit attributes and behaviors from multiple superclass's. This can be useful if you want to reuse code from multiple classes and combine their functionality in the subclass.

To use multiple inheritance in Python, you simply specify multiple superclass's in the parentheses following the name of the subclass. The subclass will then inherit attributes and behaviors from all of the superclass's.

Here is an example of multiple inheritance in Python:

class Engine:
def start(self):
print("Engine started")
class Radio:
def play(self):
print("Music playing")
class Car(Engine, Radio):
def drive(self):
print("Car is driving")
c = Car()
c.start() # Output: "Engine started"
c.play() # Output: "Music playing"
c.drive() # Output: "Car is driving"

In this example, the Car class is a subclass of both the Engine and Radio classes. It inherits the start method from the Engine class and the play method from the Radio class. It also has its own drive method.

Get Akheelmohammad’s stories in your inbox

Join Medium for free to get updates from this writer.

When you create an instance of the Car class, it will have all of the attributes and behaviors of both the Engine and Radio classes, as well as its own attributes and behaviors.

It’s important to note that Python does not support multiple inheritance with more than one direct base class. However, it is possible to use multiple inheritance with a base class and one or more mixin classes. Mixin classes are classes that are designed to provide additional functionality to a subclass without being a direct base class.

Multilevel Inheritance

Multilevel inheritance is a type of inheritance where a subclass inherits from a superclass, and that superclass also inherits from a base class. This creates a chain of inheritance, where the subclass inherits attributes and behaviors from multiple classes.

Here is an example of multilevel inheritance in Python:

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

def speak(self):
print("Animal speaking")
class Cat(Animal):
def __init__(self, name, breed):
super().__init__(name)
self.breed = breed
def speak(self):
print("Meow")
class Siamese(Cat):
def __init__(self, name, breed, toy):
super().__init__(name, breed)
self.toy = toy
def play(self):
print(f"{self.name} plays with {self.toy}")
s = Siamese("Kitty", "Siamese", "Mouse")
s.speak() # Output: "Meow"
s.play() # Output: "Kitty plays with Mouse"

In this example, the Siamese class is a subclass of the Cat class, which is itself a subclass of the Animal class. The Siamese class inherits the speak and __init__ methods from the Cat class, which in turn inherits the __init__ method from the Animal class. The Siamese class also has its own play method.

When you create an instance of the Siamese class, it will have all of the attributes and behaviors of the Animal, Cat, and Siamese classes.

Multilevel inheritance can be useful for creating a class hierarchy, where a subclass is a more specialized version of a superclass. It allows you to reuse code from multiple classes and create a more organized and maintainable codebase.

Hierarchical inheritance

Hierarchical inheritance is a type of inheritance where multiple subclasses inherit from a single superclass. This is useful if you want to create a group of subclasses that have some common attributes and behaviors, but also have their own specific attributes and behaviors.

Here is an example of hierarchical inheritance in Python:

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

def make_sound(self, sound):
print(f"This animal makes {sound}")
class Cat(Animal):
def __init__(self, name, breed, toy):
super().__init__(name, species="Cat")
self.breed = breed
self.toy = toy
def play(self):
print(f"{self.name} plays with {self.toy}")
class Dog(Animal):
def __init__(self, name, breed, toy):
super().__init__(name, species="Dog")
self.breed = breed
self.toy = toy
def fetch(self):
print(f"{self.name} fetches {self.toy}")

In this example, the Cat and Dog classes are subclasses of the Animal class. They both inherit the __init__ and make_sound methods from the Animal class, but they also have their own specific attributes and behaviors. The Cat class has the breed and toy attributes and the play method, while the Dog class has the breed and toy attributes and the fetch method.

When you create an instance of the Cat or Dog class, it will have all of the attributes and behaviors of the Animal class, as well as its own specific attributes and behaviors.

Hierarchical inheritance is a useful way to create a group of related subclasses that share common attributes and behaviors, while still allowing each subclass to have its own specific functionality.

Hybrid inheritance

Hybrid inheritance is a combination of multiple inheritance and multilevel inheritance, where a subclass inherits from multiple superclass's and those superclass's may also inherit from other classes.

Here is an example of hybrid inheritance in Python:

class Engine:
def start(self):
print("Engine started")
class Radio:
def play(self):
print("Music playing")
class Car(Engine, Radio):
def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year
def drive(self):
print("Car is driving")
class ElectricCar(Car):
def __init__(self, make, model, year):
super().__init__(make, model, year)
self.battery_size = 75
def honk(self):
print("Beep beep!")
my_car = ElectricCar("Tesla", "Model S", 2019)
my_car.start() # Output: "Engine started"
my_car.play() # Output: "Music playing"
my_car.drive() # Output: "Car is driving"
my_car.honk() # Output: "Beep beep!"

In this example, the ElectricCar class is a subclass of the Car class, which itself is a subclass of both the Engine and Radio classes. The ElectricCar class inherits the start, play, __init__, and drive methods from the Car class, and the __init__ method from the Engine and Radio classes. It also has its own honk method.

When you create an instance of the ElectricCar class, it will have all of the attributes and behaviors of the Engine, Radio, and Car classes, as well as its own honk method.

Hybrid inheritance can be useful for creating complex class hierarchies where a subclass inherits from multiple classes that may also have their own inheritance relationships. However, it can also make the class hierarchy more difficult to understand and maintain, so it should be used with caution.

Hope you understand stay tune for more………………

--

--