Inheritance in Python

Inheritance, abstraction, encapsulation, and polymorphism are the four fundamental concepts provided by OOP (Object Oriented Programming). Inheritance is a powerful feature of OOP that allows programmers to enable a new class to receive - or inherit all the properties & methods of existing class/classes. As we all came to know that class is a blueprint or template of an object. Every object is built from a class, and the concept 'inheritance' is used to create a relationship between these blueprints.

It is a feature of object-oriented programming which is used to define a new class with little or no modification to an existing class. The new class is called derived class or child class & the class from which this derived class has been inherited is the base class or parent class. The derived class is formed from base class plus it may include some extra additional features. This inheritance concept helps to reuse the code.

Inheritance

 

In the above diagram, the features of base-class are also present in the derived class along with the features of the derived class. This base-class features can be accessible to derive class because of the concept of inheritance.

The syntax of Inheritance in Python

Syntax:
class BaseClass1
 #Body of base class

class DerivedClass(BaseClass1):
 #body of derived - class

Three Ways of Parent-child Class Interaction

When programmers use this type of object-oriented concepts and reuse codes, there are three ways a parent and a child class can interact with each other. These are:

  • Anything did to the child class simply an action on the parent class.
  • Actions done on child-class overrides the actions done on parent-class.
  • Anything done on the child-class alters the action done in the parent class.

Example of Inheritance

Example:
class Person:

    def __init__(self, first, last):
        self.firstn = first
        self.lastn = last

    def Name(self):
        return self.firstn + " " + self.lastn

class Emp(Person):

    def __init__(self, first, last, staffnum):
        Person.__init__(self,first, last)
        self.staffno = staffnum

    def GetEmp(self):
        return self.Name() + ", " +  self.staffno

a = Person("Alex", "Karlos")
b = Emp("Alex", "Karlos", "A102")

print(a.Name())
print(b.GetEmp())
Output:
Alex Karlos
Alex Karlos, A102

In the above case, the object of the derived class is created and is used to invoke both of the functions of base-class as well as the derived class using a dot (.) operator. Here the result is not a part of the derived class. When the interpreter is not found in the class (derived) whose object is defined, then it continues checking that attribute in the base class. This process continues in a recursion if the base - class is itself a derived from another class.

Implicit Inheritance

Implicit actions occur in Python Inheritance when a programmer defines a function in the parent but not in the child. This type of inheritance is shown using a simple example below:

Example:
class super (object) :

    def implicit(self) :
        print ("Super-Class with Implicit function")

class sub(super) :
    pass

su = super()
sb = sub()

su.implicit()
sb.implicit()
Output:
Super-Class with Implicit function
Super-Class with Implicit function

In the above code, both objects of the base class, as well as derived class, can invoke the function of the base class. Also, the 'pass' statement under the 'sub' class is used to tell Python that the programmer wants an empty block which is created under 'sub' class, but it says there is nothing new to define in it.

The above program also shows that - if we put any function in the base-class (here 'super'), then all the derived-class (here class 'sub') will also get the features automatically from the base-class, i.e., inherit all the behavior from the parent class


Courses
Subscribe Updates via Email

Join 49,000+ W3schools lovers and get all the latest tutorials, programs, algorithms in your inbox.