In previous part of this series,
OOPs in Python Part 1, we discussed basic concepts of OOPs in Python. In this article, we will learn an essential concept of OOPs, i.e., Inheritance in Python, and its types with simple examples. Moreover, we will study Python overriding, issubclass method, and isinstance method.
Python Inheritance
Inheritance is one of the essential concepts of Python Object-Oriented programming techniques performed to achieve reuse of code. It is a mechanism to inherit new classes from existing classes and inherit their methods and attributes.
Let's start with some of the basic concepts.
Base class
In inheritance, a base class is the parent class or the super class. It can let its child classes inherit its methods and data fields depending their scope.
Derived class
A derived class is the child class that inherits methods and properties of the base class and can also have its own methods and properties. It is also called a subclass.
Syntax for Python inheritance
class BaseClass:
Base class data fields and methods (parent class block)
class DerivedClass (BaseClass):
Derived class data fields and methods (Child class block)
Example
- class dog:
- Name = ‘Dommy’
- Age = 3
- Print(“My Pet Name is” +Name, “and current Age is:”+Age)
- class My_Pet(dos):
- Color=’violet and blue’
- Print(“My pet”,+Name, “color is:”+color)
Benefits of Python Inheritance
- It reduces the size of the duplicate code declarations in a program to achieve code reusability
- It simulates real-world relationships.
- It makes the code structured and scalable.
- Code is easy to maintaned.
Types of Inheritance in Python
In Python, there are five types of inheritances available.
- Single inheritance
- Multiple inheritance
- Hybrid inheritance
- Hirarchical inheritance
- Multilevle inheritance
We will discuss them one by one.
Single Inheritance
Single inheritance is considered as one child class that is derived from only one parent class only.
Syntax
class Base_class:
<Base class statements>
class DerivedClass(Base_class):
<Derived class statements>
Example
- class parent:
- def Dog(name):
- name=name
- print("Dog name is:",name)
-
- class child(parent):
- def puppy(self,name1):
- self.name1=name1
- print("Puppy name is:",name1)
- parent.Dog ('Boxer')
-
- obj=child()
- obj.puppy ('Jimmy')
Output
Multilevel inheritance
When one class inherits from another class, each derived class will act as an intermediate base class to the next derived class. There is no limit for extending derived classes.
Syntax
class Base_class:
<base class statements>
class derived_class1(Base_class):
<Derived_class1, Base_class statements >
class derived_class2(derived_class1):
<Base_class, derived_class1, derived_class2 statements>
Example
- class class_A:
- def grandfather(self):
- gname='Raja'
- print("Grandfather Name is:",gname)
-
- class class_B(class_A):
- def father(self):
- fname='jhon'
- print("Father Name is:",fname)
-
- class class_C(class_B):
- def child(self):
- cname='Chinna'
- print("Child name is:",cname)
-
- C=class_C()
- C.grandfather()
- C.father()
- C.child()
Output
Python can support multiple inheritances, when a new derived class inherits features from more than one base classes. For example, a child can get attributes from his father and mother.
The syntax of multiple inheritance is given below.
Syntax
class base_A:
<class suit>
class base_B:
<class suit>
class derivedclass_C(base_A,base_B):
<class suit>
Example
- class class_A():
- def male(self,name,age):
- self.name=name
- self.age=age
- print("The Male Name is: %s and Age is: %d" %(name,age))
-
- class class_B():
- def female(self,name,age):
- self.name=name
- self.age=age
- print("The Female Name is: %s and Age is: %d" %(name,age))
-
- class class_C(class_A,class_B):
- def human(self,place1,place2):
- self.place1=place1
- self.place2=place2
- print("Humans possible to live in: %s and Age is: %s" % (place1,place2))
-
- obj=class_C()
- obj.male('Chinna',21)
- obj.female('Bharathi',2)
- obj.human('Earth','Moon')
Output
Hierarchical Inheritance
A hierarchical inheritance works well in python like other object-oriented languages. When more than one derived class extended from one base class consider as a hierarchical inheritance.
Syntax
class class_A:
<Base class statements>
class class_B(class_A):
<class_B and class_A statements>
class class_C(class_A):
<class_C and class_A statements>
Example
- class class_A:
- def base():
- print("This is a Base class")
-
-
- class class_B(class_A):
- def derived_1(self):
- class_A.base()
- print("This is a Derived class-1 ")
-
-
- class class_C(class_A):
- def derived_2(self):
- class_A.base()
- print("This is a Derived class-2")
-
- obj1=class_B()
- obj2=class_C()
-
- obj1.derived_1()
- obj2.derived_2()
Output
Hybrid inheritance
Python provides us the hybrid inheritance for handling more than one type of inheritance in a program. A combination of more than one inheritance, multilevel and hierarchical inheritance work together (inherit its properties) in a program called as Hybrid inheritance.
Let’s see a syntax for hybrid inheritance.
Syntax
class class_A:
<class-suit>
class class_B(class_A):
<class-suit>
class class_C(class_B):
<class-suit>
class class_D(class_B):
<class-suit>
Example
- class A:
- def summation(a,b):
- return a+b
-
- class B(A):
-
- def subtract(a,b):
- x=a-b
- print("The subtract value is:",x)
- print("Summation value is:",A.summation(20,30))
-
-
- class C(B):
-
- def multiplication(self,a,b):
- B.subtract(10,20)
- y=a*b
- print("Multiplication value is:",y)
-
- class D(B):
- def divide(self,a,b):
- B.subtract(50,30)
- z=a/b
- print("Divide value is:",z)
-
- obj1=C()
- obj2=D()
- obj1.multiplication(10,20)
- print(" ")
- obj2.divide(30,5)
Output
Python Method overriding
In method overriding, a derived class can override a method of a base class, and add more functionality to it.
Limitation of overriding
- Function overriding does not perform in the same class.
- It must be in the same number of names and parameters.
Example 1
- class parent:
- def display(self):
- print("Befor override")
- class child(parent):
- def display(self):
- print("After override")
- d = child()
- d.display()
Here, we hve created two classes that simulate parent and child functionality.
Then also created two methods itself each that represented the same name such as display().
Next, I created an object for the child class that extended from the parent class.
When execute the print statement under the display() method, it will override the class parent statement and display that. Given below
Output
Example 2 - Realtime example of overriding
Let’s take a look at this simple example to clearly understand method overriding.
- class car:
- def car_color(self):
- a='gray_color'
- return a;
- class toyota(car):
- def car_color(self):
- b='black_color'
- return b;
- class audi(car):
- def car_color(self):
- c='blue_color'
- return c;
-
- c1=car()
- c2=toyota()
- c3=audi()
-
- print("Mostly liked car color is:",c1.car_color())
- print("Toyata car color is:",c2.car_color())
- print("Audi car color is:",c3.car_color())
Output
Python issubclass() method
The “issubclass()” method used to find the relationship of the specified classes. It will take an object and subclass name in the parameter to return output as True or False. If it returns True, we confirm the given an object is a subclass of the specified object. Otherwise False.
Example
- class fruit1:
- def apple(self):
- a='green_color'
- return a;
- class fruit2:
- def lemon(self):
- b='yello_color'
- return b;
- class fruit3:
- def blueberry(self):
- c='Blue_color'
- return c;
- class derived(fruit1,fruit2,fruit3):
- def derived(sself):
- d='RGB'
- return d;
-
- d=derived()
- print(issubclass(derived,fruit1))
- print(issubclass(fruit1,fruit2))
- print(issubclass(derived,fruit3))
Python isinstance() method
Python can be available to check the relationship between the objects and classes by using “isinstance()” method.
Example
- class calculation1:
- def add(self,x,y):
- return x+y;
- class calculation2:
- def sub(self,x,y):
- return x-y;
- class derived(calculation2):
- def divide(self,x,y):
- return x/y;
-
- d=derived()
- c=calculation2()
- print(isinstance(d,derived))
-
- print(isinstance(d,calculation1))
-
- print(isinstance(d,calculation2))
Output
I hope, you understood very well about the Inheritance and its types. If you are getting any queries while doing above mentioned, feel free to comment below, and stay tuned, we will see the rest of OOPs concepts in the next article.