In this tutorial, we’ll be learning how to perform Inheritance in Python.
When you’re trying to solve some coding problem, often you’ll find an existing class that creates objects that do almost what you need. What can you do?
You could modify this old class, but you’ll make it more complicated, and you might break something that used to work.
Or you could write a new class, cutting and pasting from the old one and merging your new code. But this means that you have more code to maintain, and the parts of the old and new classes that used to work the same might drift apart because they’re now in separate places.
One solution is Inheritance: creating a new class from an existing class, but with some additions or changes. It’s a good way to reuse code. When you use inheritance, the new class can automatically use all the code from the old class but without you needing to copy any of it.
Inherit from a Parent Class
You define only what you need to add or change in the new class, which overrides the old class’s behavior. The original class is called a parent, superclass, or base class; the new class is called a child, subclass, or derived class. These terms are interchangeable in object-oriented programming.
Let’s Code Inheritance in Python
Let’s inherit something. In the next example, we define an empty class called Car. Next, we define a subclass of Car called Details. You define a subclass by using the same class keyword but with the parent class name inside the parentheses (class Details(Car) here):
>>>class Car(): … pass … >>>class Details(Car): … pass …
You can check whether a class is derived from another class by using issubclass():
>>>issubclass(Details, Car) True
Next, create an object from each class:
>>>give_me_a_car = Car() >>>give_me_a_details = Details()
A child class is a specialization of a parent class; in object-oriented lingo, Details is-a Car. The object named give_me_a_details is an instance of class Details, but it also inherits whatever a Car can do. In this case, Car and Details are as useful as deckhands on a submarine, so let’s try new class definitions that actually do something:
>>>class Car(): … def exclaim(self): … print("I'm a Car!") … >>>class Details(Car): … pass …
Finally, make one object from each class and call the exclaim method:
>>>give_me_a_car = Car() >>>give_me_a_details = Details() >>>give_me_a_car.exclaim() I'm a Car! >>>give_me_a_details.exclaim() I'm a Car!
Without doing anything special, Details inherited the exclaim() method from Car.
Example of Inheritance in Python
class Car: def __init__(self, comapnyName, carModel): self.companyname = companyName self.carmodel = carModel def printname(self.companyname, self.carmodel) class Details(Car): pass x = Details("Audi", "A8") x.printname() >>>('Audi','A8')
Inheritance is a concept of OOPS programming languages. Inheritance can very useful when programmers want to access objects from different classes without deriving them. If Inheritance is not used properly, it may cause an Identity Crisis.