Inheritance in Python With Examples

Inheritance is one of the most important parts of the OOPs concept. The process of acquiring all the properties and behavior of the parent class by a child class is known as inheritance. The child class can inherit all the property of the parent class by using inheritance. The class which acquires all the .property and behavior from another class is known as a derived class or child class. The class from which the properties and behaviors are inherited is known as the parent class or base class. One of the biggest advantages of inheritance is the re-usability of code.

Key features of Python Inheritance:

  • Using inheritance the real-world entity-relationship can be represented well.
  • The main feature of inheritance is to reuse the code. Reusability is one of the greatest features of the OOPs concept.  Programmers do not need to write the same code again and again. It can be reduced by the feature of the reusability of code.
  • Inheritance property belongs to transitive nature which means child class can access all the properties of the parent class automatically.

Definition about object class: 

The OOPs concept of Python is very much similar to the Java programming language. Like Java, the object is also the root or parent class of all classes. The child class should know about its parent class. For this purpose, the name of the parent class is defined in the child class definition.

Sample code:

class School (object): # Create parent class and object is the root or class of all class
     def schoolName (self):
         print("School name is XYZ")

class Student (Schoo ): # Child class inherits all the properties of  School class
     def studentName (self):
         print("Student name is Jack")

class Teacher (School): # Child class inherits all the properties of  School class
      def teacherName (self):
         print("Teacher name is Mr. Smith")


s1  = Student()  # Creating object of Student child class
t1 = Teacher() # Creating object of Teacher child class

# Using object accessing the property of School parent class
s1. schoolName()
s1. studentName()

# Using object accessing the property of School parent class
t1. schoolName() 
t1.teacherName()


# Output
# School name is XYZ
# Student name is Jack

# School name is XYZ 
# Teacher name is Mr. Smith

Method Overriding in Python:

The method overriding is a process of implementing a method in child class that method already exists in the parent class. By doing this we can provide specific information to the parent class method. This method overriding process is needed when different definitions are required of a parent class method. In the method overriding process, the method name of the child class must be the same as the method name of the parent class. In the child class method more specific information is given than the parent class method.

Sample Code:

class Car (object): # Create parent class and object is the root or class of all class
     def drive (self):
         print("Every car has wheels")

class Maruti (Car): # Child class inherits all the properties of Car class
     def drive (self):
         print("This car has 4 wheels")

c1  = Maruti()  # Creating object of Maruti child class

# Using object accessing the property of Car parent class
c1. drive ()  

# Output
# This car has 4 wheels