Inheritance

Inheritance is a feature of object-oriented programming. It specifies that one object acquires all the properties and behaviors of parent object. By using inheritance you can define a new class with a little or no changes to the existing class. The new class is known as derived class or child class and from which it inherits the properties is called base class or parent class.

It provides re-usability of the code.

Inheritance can be categorised into five types:

  1. Single Inheritance

  2. Multilevel Inheritance

  3. Multiple Inheritance

  4. Hierarchical Inheritance

  5. Hybrid Inheritance

# Inheritance Syntax

''' Note object in bracket. 
 (Generally, object is made ancestor of all classes) 
 In Python 3.x "class Person" is  
 equivalent to "class Person(object)"
 But in Python 2.x it is necessary to declare it explicitly'''

class BaseClass(object):
  # Statements of BaseClass go here
  pass

class DerivedClass(BaseClass):
  # Statements of BaseClass go here
  pass

obj1 = BaseClass()
obj2 = DerivedClass()

print (type(obj1))
print (type(obj2))

4.1 Single Inheritance

When a child class inherits from only one parent class, it is called as single inheritance.

alt text

In python the above task of extending init () can be achieved the following ways:

  • By using the super() function

  • By using the name of the super class.

Method 1: super() function:

In Python, super() function is used to call the methods of base class which have been extended in derived class.

We do not need to specify the name of the base class if we use super(), hence we can easily change the base class for Dog method easily.

Method 2: By using name of the super class

In Python, name of the base class can also be used to access the method of the base class which has been extended in derived class

4.2 Multiple Inheritance

Like C++, a class can be derived from more than one base classes in Python. This is called multiple inheritance.

In multiple inheritance, the features of all the base classes are inherited into the derived class. The syntax for multiple inheritance is similar to single inheritance.

alt text

4.3 Multilevel Inheritance

We can also inherit form a derived class. This is called multilevel inheritance. It can be of any depth in Python.

In multilevel inheritance, features of the base class and the derived class is inherited into the new derived class.

alt text

Some other types are:

Hierarchical Inheritance :

More than one derived classes are created from a single base.

alt text

Hybrid Inheritance:

This form combines more than one form of inheritance. Basically, it is a blend of more than one type of inheritance.

alt text

4.4 Method Overriding

The feature of overriding methods enables the programmer to provide specific implementation to a method in the subclass which is already implemented in the superclass. The version of a method that is executed will be determined by the object that is used to invoke it. If an object of a parent class is used to invoke the method, then the version in the parent class will be executed, but if an object of the subclass is used to invoke the method, then the version in the child class will be executed.

In Python method overriding occurs simply defining in the child class a method with the same name of a method in the parent class. When you define a method in the object you make this latter able to satisfy that method call, so the implementations of its ancestors do not come in play.

4.5 Abstract Methods

An abstract method is a method defined in a base class, but that may not provide any implementation. In Java, it would describe the methods of an interface.

An abstract method is provided by the (abstract) parent class but only implemented in the children classes.

So the simplest way to write an abstract method in Python is:

Any class inheriting from Pizza should implement and override the get_radius method, otherwise an exception would be raised.

This particular way of implementing abstract method has a drawback. If you write a class that inherits from Pizza and forget to implement get_radius, the error will only be raised when you'll try to use that method.

The method of implementing abstract methods by using the "NotImplementedError" exception has a drawback

If you write a class that inherits from circle and forget to implement get_radius, the error will only be raised when you'll try to use that method.

Last updated

Was this helpful?