Inheritance in Python
Inheritance refers to a class’s ability to derive or inherit properties from another class. It accurately depicts real-world relationships. It ensures that code can be reused. We don’t have to rewrite the same code over and over. It also allows us to add new features to a class without changing it. It is transitive, which means that if class B inherits from another class A, then all of B’s subclasses will also inherit from class A.
Generating a Parent Class
Inheritance is an object-oriented programming concept in Python that allows a class to derive the properties and behavior of another Python class. The class that is being inherited is called the superclass or parent class, while the class that inherits from the superclass is called the subclass or child class.
Inheritance is an important concept in Python because it helps in code reusability, reduces redundancy, and promotes modular programming. With inheritance, you can create a new class that is a modified or enhanced version of an existing class. You can then reuse the code in the existing class in the new class, thereby avoiding the need to write the same code again.
Such as making a Student class that includes Display methods. It should be the parent class.
Example:
class Student(object):
def __init__(self, name, id):
self.name = name
self.id = id
def display(self):
print(self.name, self.id)
stu_object = Student("Rahim", 12127781)
stu_object.display()
Output:
Rahim 12127781
Generating a Child Class
In this case, Math is another class that will inherit the properties of the Student class (base class).
Example:
class Student(object):
def __init__(self, name, id):
self.name = name
self.id = id
def display(self):
print(self.name, self.id)
stu_object = Student("Rahim", 12127781)
stu_object.display()
class Math(Student):
def p_rint(self):
print("Math class called")
Details = Math("Karim", 12127790)
Details.display()
Details.p_rint()
Output:
Rahim 12127781
Karim 12127790
Math class called
Python Inheritance Example
Inheritance refers to the process of passing on the properties of a parent class to a child class. The existing class is referred to as the base class or parent class, and the new class is referred to as the subclass, child class, or derived class. In this Python tutorial, you will learn about inheritance, method overloading, method overriding, inheritance types, and MRO (Method Resolution Order). In Object-oriented programming, inheritance is crucial. The primary goal of inheritance is code reusability because we can use an existing class to create a new class rather than starting from scratch. In inheritance, the child class inherits all of the parent class’s data members, properties, and functions. A child class can also provide its own implementation of the parent class’s methods.
Example:
class Student(object):
def __init__(self, name):
self.name = name
def getName(self):
return self.name
def isTeacher(self):
return False
class Teacher(Student):
def isTeacher(self):
return True
details = Student("Rahim")
print(details.getName(), details.isTeacher())
details = Teacher("Amit")
print(details.getName(), details.isTeacher())
Output:
Rahim False
Amit True
Subclassing (Calling constructor of parent class)
A child class must determine which class is its parent class. This is accomplished by including the parent class name in the definition of the child class.
Example:
class Student(object):
def __init__(self, name, mark, idnumber):
self.name = name
self.idnumber = idnumber
self.mark = mark
def display(self):
print(self.name, self.mark, "to ID:" ,self.idnumber)
class Teacher(Student):
def __init__(self, name, idnumber, mark):
Student.__init__(self, name, mark, idnumber)
details = Teacher('Dr. Mizan gives mark: ', 12127781, 92)
details.display()
Output:
Dr. Mizan gives mark: 92 to ID: 12127781
A single inheritance, Single inheritance occurs when a child class inherits from only one parent class. Above is an example. Multiple inheritances, Multiple inheritances occur when a child class inherits from multiple parent classes.
In Python, a subclass can inherit all the attributes and methods of its superclass, and it can also override or modify these attributes and methods to suit its specific needs. This flexibility allows you to create specialized versions of existing classes, tailored to the requirements of a specific problem or use case.
Inheritance is an important feature of object-oriented programming in Python that allows you to create modular, reusable, and extensible code.