Python Inheritance

The purpose of this lesson is to provide an understanding of Python inheritance by using examples.

Syntax:

Class BaseClass:
    {Body}
Class DerivedClass(BaseClass):
    {Body}

 

Python Inheritance



Python Inheritance

When we talk about Python inheritance, it allows us to create a class that inherits all its methods and properties from another class.

The parent class is called the base class because it is the class that is being inherited from.

Classes that inherit from another class are called child classes or derived classes.


Python Class Inheritance:

  1. Class inheritance allows you to reuse code from existing classes. By creating a new class that inherits from a base class, you can inherit attributes and methods from the base class, reducing code duplication and promoting modularity.
  2. Inheritance enables you to create specialized classes that inherit and extend the functionality of a base class. You can add new attributes and methods to the derived class or override existing ones to provide specialized behavior. This allows for flexible and customizable class hierarchies.
  3. Inheritance plays a crucial role in achieving polymorphism in Python. Polymorphism allows objects of different classes to be treated interchangeably based on a common interface or shared base class. This promotes code flexibility and simplifies code logic by allowing the same methods to be called on different objects with varying implementations.
  4. Inheritance helps in organizing code by creating a hierarchical structure of related classes. It allows you to model real-world relationships and abstract common attributes and behaviors into a base class. This promotes code readability, maintainability, and understanding of the underlying structure.
  5. Python supports multiple inheritance, which means a derived class can inherit from multiple base classes. This feature allows you to combine and inherit features from multiple sources, providing even greater flexibility and code reuse. However, it requires careful management to avoid ambiguity or conflicts in method resolution order.

Creating a Parent Class

When it comes to Python Class inheritance, any class can be a parent class, so the syntax is identical.

Make a class named Address, with city_name and country_name properties, and a print_address:

Python Parent Class Example: 1 

class Address: def __init__(mrx, city, karachi): mrx.city_name = city mrx.country_name = karachi def print_address(mrx): print(mrx.city_name, mrx.country_name) #Apply the Address class, create an object and invoke the print_address method: x = Address("New York,", " United States of America") x.print_address()

Now make a class named Student_detail, with s_id, s_name and s_age properties, and a print_detail method:

Python Parent Class Example: 2 

class Student_detail: def __init__(mrx, id, name, age): mrx.s_id = id mrx.s_name = name mrx.s_age = age def print_detail(mrx): print(mrx.s_id, mrx.s_name, mrx.s_age) x = Student_detail("20499 ", "Harry Maguire ", 19) x.print_detail()

Creating a Child Class

When dealing with Python class inheritance, the parent class is passed as a parameter when creating a child class so functionality can be inherited from it.

Make a class called Records that inherits the attributes and functions of the Student_detail class:

Python Child Class Example: 1 

class Student_detail: def __init__(mrx, id, name, age): mrx.s_id = id mrx.s_name = name mrx.s_age = age def print_detail(mrx): print(mrx.s_id, mrx.s_name, mrx.s_age) class Records(Student_detail): pass x = Student_detail("20499 ", "Harry Maguire ", 19) x.print_detail()

Reminder: When you don’t need to provide the class any new methods or attributes, use the pass keyword.

Therefore, the Student_detail class’s properties and methods are shared by the Records class as well.

Now, Create an object using the Records class, and then call the print_detail function on the object:

Python Child Class Example: 2 

class Student_detail: def __init__(mrx, id, name, age): mrx.s_id = id mrx.s_name = name mrx.s_age = age def print_detail(mrx): print(mrx.s_id, mrx.s_name, mrx.s_age) class Records(Student_detail): pass x = Records("20499 ", "Harry Maguire ", 19) x.print_detail()

Make a class called Courses that inherits the attributes and functions of the Student_detail class:

Python Child Class Example: 3 

class Student_detail: def __init__(mrx, id, name, age): mrx.s_id = id mrx.s_name = name mrx.s_age = age def print_detail(mrx): print(mrx.s_id, mrx.s_name, mrx.s_age) class Courses(Student_detail): pass x = Student_detail("20499 ", "Harry Maguire ", 19) x.print_detail()

Reminder: When you don’t need to provide the class any new methods or attributes, use the pass keyword.

Therefore, the Student_detail class’s properties and methods are shared by the Courses class as well.

Create an object using the Courses class, and then call the print_detail function on the object:

Python Child Class Example: 4 

class Student_detail: def __init__(mrx, id, name, age): mrx.s_id = id mrx.s_name = name mrx.s_age = age def print_detail(mrx): print(mrx.s_id, mrx.s_name, mrx.s_age) class Courses(Student_detail): pass x = Courses("20499 ", "Harry Maguire ", 19) x.print_detail()

 


Add __init__() Function

As of now, we have developed a child class in Python inheritance that receives properties and methods from its parent.

To the child class, we want to add the __init__() function (instead of the pass keyword).

Reminder: When the class is used to create a new object, the __init__() function is immediately invoked.

Insert the __init__() method to the Records class:

__init__() Function Example: 1 

class Student_detail: def __init__(mrx, id, name, age): mrx.s_id = id mrx.s_name = name mrx.s_age = age def print_detail(mrx): print(mrx.s_id, mrx.s_name, mrx.s_age) class Records(Student_detail): def __init__(mrx, id, name, age): Student_detail.__init__(mrx, id, name, age) #insert properties etc. x = Records("20499 ", "Harry Maguire ", 19) x.print_detail()

The child class will no longer inherit the parent’s __init__() function if you insert the __init__() function.

The __init__() method of the child overrides the inheritance of the __init__() method of the parent.

Add a call to the parent’s __init__() method to maintain the inheritance of the parent’s __init__() function:

__init__() Function Example: 2 

class Student_detail: def __init__(mrx, id, name, age): mrx.s_id = id mrx.s_name = name mrx.s_age = age def print_detail(mrx): print(mrx.s_id, mrx.s_name, mrx.s_age) class Records(Student_detail): def __init__(mrx, id, name, age): Student_detail.__init__(mrx, id, name, age) x = Records("20499 ", "Harry Maguire ", 19) x.print_detail()

__init__() Function Example: 3 

class Student_detail: def __init__(mrx, id, name, age): mrx.s_id = id mrx.s_name = name mrx.s_age = age def print_detail(mrx): print(mrx.s_id, mrx.s_name, mrx.s_age) class Courses(Student_detail): def __init__(mrx, id, name, age): Student_detail.__init__(mrx, id, name, age) x = Courses("20499 ", "Harry Maguire ", 19) x.print_detail()

Now that the __init__() function has been properly implemented and the parent class’ inheritance has been maintained, we can start adding functionality to the function.


Using super() Function

When it comes to Python inheritance, there is also a super() function that will allow the child class to inherit all of the methods and properties from its parent:

Super() Function Example: 1 

class Student_detail: def __init__(mrx, id, name, age): mrx.s_id = id mrx.s_name = name mrx.s_age = age def print_detail(mrx): print(mrx.s_id, mrx.s_name, mrx.s_age) class Records(Student_detail): def __init__(mrx, id, name, age): super().__init__(id, name, age) x = Records("20499 ", "Harry Maguire ", 19) x.print_detail()

Super() Function Example: 2 

class Student_detail: def __init__(mrx, id, name, age): mrx.s_id = id mrx.s_name = name mrx.s_age = age def print_detail(mrx): print(mrx.s_id, mrx.s_name, mrx.s_age) class Courses(Student_detail): def __init__(mrx, id, name, age): super().__init__(id, name, age) x = Courses("20499 ", "Harry Maguire ", 19) x.print_detail()

You do not need to specify the parent element’s name when using the super() function because the child element will immediately take its parent’s methods and properties.


Add Properties

Create a property named cgpa in the Records class:

Python Inheritance Add Property Example: 1 

class Student_detail: def __init__(mrx, id, name, age): mrx.s_id = id mrx.s_name = name mrx.s_age = age def print_detail(mrx): print(mrx.s_id, mrx.s_name, mrx.s_age) class Records(Student_detail): def __init__(mrx, id, name, age): super().__init__(id, name, age) mrx.cgpa = 3.67 x = Records("20499 ", "Harry Maguire ", 19) x.print_detail() print(x.cgpa)

Create a property named course_name in the Courses class:

Python Inheritance Add Property Example: 2 

class Student_detail: def __init__(mrx, id, name, age): mrx.s_id = id mrx.s_name = name mrx.s_age = age def print_detail(mrx): print(mrx.s_id, mrx.s_name, mrx.s_age) class Courses(Student_detail): def __init__(mrx, id, name, age): super().__init__(id, name, age) mrx.course_name = "Object Oriented Programming" x = Courses("20499 ", "Harry Maguire ", 19) x.print_detail() print(x.course_name)

When creating records objects, the cgpa 3.67 and course_name is Object Oriented Programming should be a variable included into the Records and Courses class respectively.

You can do this by adding another parameter to the init() method.

When creating objects, provide a cgpa parameter and pass the appropriate cgpa:

Python Inheritance Add Property Example: 3 

class Student_detail: def __init__(mrx, id, name, age): mrx.s_id = id mrx.s_name = name mrx.s_age = age def print_detail(mrx): print(mrx.s_id, mrx.s_name, mrx.s_age) class Records(Student_detail): def __init__(mrx, id, name, age,cgpa): super().__init__(id, name, age) mrx.cgpa_num = cgpa x = Records("20499 ", "Harry Maguire ", 19, 3.67) x.print_detail() print(x.cgpa_num)

When creating objects, provide a course parameter and pass the appropriate course:

Python Inheritance Add Property Example: 4 

class Student_detail: def __init__(mrx, id, name, age): mrx.s_id = id mrx.s_name = name mrx.s_age = age def print_detail(mrx): print(mrx.s_id, mrx.s_name, mrx.s_age) class Courses(Student_detail): def __init__(mrx, id, name, age, course): super().__init__(id, name, age) mrx.course_name = course x = Courses("20499 ", "Harry Maguire ", 19, "Object Oriented Programming") x.print_detail() print(x.course_name)

Add Methods

Include a function called total_marks in the Records class:

Add Method Example: 1 

class Student_detail: def __init__(mrx, id, name, age): mrx.s_id = id mrx.s_name = name mrx.s_age = age def print_detail(mrx): print(mrx.s_id, mrx.s_name, mrx.s_age) class Records(Student_detail): def __init__(mrx, id, name, age,cgpa): super().__init__(id, name, age) mrx.cgpa_num = cgpa def total_marks(mrx, marks): print(f"Total Marks of {mrx.s_id}-{mrx.s_name}-{mrx.s_age} is {marks} and CGPA is {mrx.cgpa_num}") x = Records("20499", "Harry Maguire", 19, 3.67) x.total_marks(95)

Include a function called course_marks in the Courses class:

Add Method Example: 2 

class Student_detail: def __init__(mrx, id, name, age): mrx.s_id = id mrx.s_name = name mrx.s_age = age def print_detail(mrx): print(mrx.s_id, mrx.s_name, mrx.s_age) class Courses(Student_detail): def __init__(mrx, id, name, age, course): super().__init__(id, name, age) mrx.course_name = course def course_marks(mrx, marks): print(f"{mrx.s_id}-{mrx.s_name}-{mrx.s_age} your marks in {mrx.course_name} is {marks}, Out of 1000") x = Courses("20499 ", "Harry Maguire ", 19, "Object Oriented Programming") x.course_marks(786)

Python Inheritance examples

In the child class, you can override the inheritance of the parent method if the method in the child class has the same name as the function in the parent class.


Python Inheritance Benefits

  1. Code reusability is made possible since a child class inherits all the functionalities of its parent class.

  2. Functionality can be inherited once it has been developed. The wheel does not need to be reinvented. As a result, the code is cleaner and easier to maintain.

  3. You can even define your own functionalities for the child class, so only essential functions will be inherited and other required functions will be defined.

 We appreciate your reaction and encourage you to leave your thoughts below to help us improve this site.
We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

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