Python Inheritance

1.Inheritance is one of the important features of OOP(Object-Oriented-Program).

2.The process in which the properties of one class is transferring to another class is known as inheritance.

3.The properties are transferring from which class is known as parent class.

4.The class which is inheriting from the parent class is known as child class.

4.Inheritance provides the concept of re-usability which is the main advantage.


Example 1

class Parent:
def f1(self):
	print("Python Programming")

class Child(Parent):
def f2(self):
	print("Python for DataScience")

obj=Child()
obj.f1()
obj.f2()

Output:
Python Programming
Python for DataScience

Note: Here we observed that the child class object can access parent class property and it’s own property. In the above program, the name of the parent class is Parent and child class is Child. The object obj is the Child class type of object. Through the Child class object obj, We have accessed f1() and f2(). The function f1() is the property of Parent class and the function f2() is the property of Child class.


Example 2

class A:
	x=100

class B(A):
	y=200

def show(self):
	print("x=",self.x)
	print("y=",self.y)

ob=B()
ob.show()

Output:
x= 100
y= 200

Note: Here from the above we observed that the child class property can access the parent class property also.


Function Overriding

When a function and it's signature in a parent class and the function and its signature in child class is exactly the same, then the function in the child class is called a function overridden. This is known as function overriding.

class Rectangle:
	  def area(self,x,y):
		  self.x=x
		  self.y=y
		  a=self.x * self.y
		  print("area of rectangle is",a)

class Triangle(Rectangle):
	  def area(self,x,y):
	  self.x=x
	  self.y=y
	  a=0.5 * self.x * self.y
	  print("area of triangle is",a)

obj=Triangle()
obj.area(5,6)

Output
area of the triangle is 15.0

Note: From the above program the area() in child class Triangle is called an overridden function. Here we observed the child class property is coming to the picture and parent class property is not coming to the picture. So to eliminate this disadvantage we will use super() in child class.


use of super()

In Python inheritance context, we use super() to access parent(super) class property and the parent class property is invoked by the super() in child class. Basically we use super() in the child class, when the parent class and child class property is exactly the same. Consider the above function overriding example;

class Rectangle:
	  def area(self,x,y):
	      self.x=x
	      self.y=y
	      a=self.x * self.y
	      print("area of rectangle is",a) 

class Triangle(Rectangle):
	  def area(self,x,y):
	      self.x=x
	      self.y=y
	      a=0.5 * self.x * self.y

super().area(4,5)
print("area of triangle is",a)

obj=Triangle()
obj.area(5,6)

Output:
area of the rectangle is 20
area of the triangle is 15.0


About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc






 PreviousNext