Startertutorials Blog
Tutorials and articles related to programming, computer science, technology and others.
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.

Categories: Python Programming. No Comments on Inheritance and Polymorphism in Python
0
(0)

In this article we will learn about inheritance and polymorphism in Python programming language. Both inheritance and polymorphism are defined and examples are provided.

 

Inheritance

Creating a new class from existing class is known as inheritance. The class from which features are inherited is known as base class and the class into which features are derived into is called derived class.


Subscribe to our monthly newsletter. Get notified about latest articles, offers and contests.


 

Inheritance promotes reusability of code by reusing already existing classes. Inheritance is used to implement is-a relationship between classes.

 

Following hierarchy is an example representing inheritance between classes:

 

vehicle-hierarchy

 

Syntax for creating a derived class is as follows:

class  DerivedClass(BaseClass):
	statement1
	statement2
	...
	statementN

 

Following example demonstrates inheritance in Python:

class  Shape:
	def  __init__(self): print("Shape Constructor")
	def  area(): print("Area of shape")
	def  peri(): print("Perimeter of shape")
class  Circle(Shape):
	def  __init__(self, r):
		Shape.__init__(self) #Calling parent constructor
		self.radius = r
		print("Circle Constructor")
	def  area(self): print("Area of circle is:", 3.142*self.radius*self.radius)
c = Circle(5)
c.area()

 

Polymorphism

Polymorphism means having different forms. Based on the context, a variable, function, or an object can have different meaning. As inheritance is related to classes, polymorphism is related to methods. In Python, method overriding is one way of implementing polymorphism.

 

Method Overriding

In method overriding, a base class and the derived class will have a method with the same signature. The derived class method can provide different functionality when compared to the base class method.

 

When a derived class method is created and the method is invoked, the derived class method executes overriding the base class method. Following example demonstrates method overriding:

class  Shape:
	def  __init__(self): print("Shape Constructor")
	def  area(self): print("Area of shape")
	def  peri(self): print("Perimeter of shape")
class  Circle(Shape):
	def  __init__(self, r):
		Shape.__init__(self) #Calling parent constructor
		self.radius = r
		print("Circle Constructor")
	def  area(self):
		print("Area of circle is:", 3.142*self.radius*self.radius)
c = Circle(5)
c.area() #Circle class area method overrides Shape class area method

 

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Suryateja Pericherla

Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.

He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.

He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.

Leave a Reply

Your email address will not be published. Required fields are marked *