In Python, inheritance is the process of creating a new class that is a modified version of an existing class. The new class, called the child class or subclass, inherits the properties and methods of the existing class, called the parent class or superclass.
To create a subclass in Python, you define a new class and specify the name of the parent class in parentheses after the class name:
class ParentClass:
def method1(self):
print("Parent method 1")
def method2(self):
print("Parent method 2")
class ChildClass(ParentClass):
def method2(self):
print("Child method 2")
In this example, we define a parent class called ParentClass with two methods
method1() and method2(). We then define a child class called
ChildClass that inherits from ParentClass using the syntax
class ChildClass(ParentClass):.
The ChildClass overrides the implementation of method2() from
ParentClass by defining its own implementation of the method. It does not modify the implementation of
method1() from the parent class.
To create an instance of the ChildClass, you simply call the constructor of the class:
child = ChildClass()
In Python, you can also use the super() function to call a method in the parent class from within a method in the child class:
class ChildClass(ParentClass):
def method2(self):
super().method2() # call the parent class implementation of method2
print("Child method 2")
In this example, the method2() of the ChildClass calls the parent class implementation of the method using the
super() function before printing its own message.
Inheritance allows for one class to gain all the attributes and methods of another class. code reusability increase in Inheritance. it makes easier to create and maintain an application. The classfrom which we are inheriting is known as super-class and the class that is inherited is called a child class.
Type of Inheritance in Python :
- Single Inheritance
- Multi-level inheritance
- Hierarchical inheritance
- Multiple inheritance
Join MindStick Community
You need to log in or register to vote on answers or questions.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
In Python, inheritance is the process of creating a new class that is a modified version of an existing class. The new class, called the child class or subclass, inherits the properties and methods of the existing class, called the parent class or superclass.
To create a subclass in Python, you define a new class and specify the name of the parent class in parentheses after the class name:
In this example, we define a parent class called ParentClass with two methods method1() and method2(). We then define a child class called ChildClass that inherits from ParentClass using the syntax class ChildClass(ParentClass):.
The ChildClass overrides the implementation of method2() from ParentClass by defining its own implementation of the method. It does not modify the implementation of method1() from the parent class.
To create an instance of the ChildClass, you simply call the constructor of the class:
In Python, you can also use the super() function to call a method in the parent class from within a method in the child class:
In this example, the method2() of the ChildClass calls the parent class implementation of the method using the super() function before printing its own message.
Inheritance allows for one class to gain all the attributes and methods of another class. code reusability increase in Inheritance. it makes easier to create and maintain an application. The classfrom which we are inheriting is known as super-class and the class that is inherited is called a child class.
Type of Inheritance in Python :
- Single Inheritance
- Multi-level inheritance
- Hierarchical inheritance
- Multiple inheritance