Date:

Share:

How to Implement Inheritance in Python

Related Articles

In this article I will explain how to implement a legacy in Python. In short, inheritance allows the girl class to inherit the characteristics of the parent class. While the parent class or base class is the class that is inherited. While the child class or sub-class is the class that inherits characteristics from the base class.

Creating the base class

We create the base class as a standard class containing attributes, methods and the constructor. The __init __ () function is similar to a constructor. Like a C # class builder, it initializes the state of the object. Also, a python class can use myself A keyword that represents the class’ instance. Therefore we can use the self-keyword to access the features and methods of a class.

Admittedly, we can create class attributes within the __init __ () function. However, we can use these attributes only through the object of the same class.

Creating a subdivision in Python

In order to create a subclass in python, we need to enclose the base class name in parentheses that follows the subname. For example, we can create a subdivision as follows:

class subclass_name (baseclass_name):

The following example demonstrates how to create sub-classes in python. As you can see, the class is named My class Is the base department. True, the lessons MySubClass1 and MySubClass2 They are derivative classes. Moreover, the base class contains three methods. It also has two show features – x, and y.

Using the keyword pass

in principle, pass Represents a null statement in Python. When this statement is encountered, nothing happens. The following MySubClass2 setting indicates that this class no longer adds to its setting.

Workaround in Python

As can be seen in the definition of MyClass and MySubClass2, these two classes contain the show () method. Therefore, the show () method in MySubClass2 bypasses the definition of show () set in MyClass.

class MyClass:
	def __init__(self, x, y):
		print("__init__() Function of Parent Class...")
		self.x=x
		self.y=y

	def showSum(self):
		print("Sum = ", self.x+self.y)

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

class MySubClass1(MyClass):
	pass

class MySubClass2(MyClass):
	def __init__(self, x, y, z):
		super().__init__(x,y)
		print("__init__() function of Sub Class 2...")
		self.z=z
	def ThreeSum(self):
		print("Sum of 3 = ", self.x+self.y+self.z)

	def ThreeProduct(self):
		print("Product of 3 = ", self.x*self.y*self.z)
	def show(self):
		print("x = ", self.x, " y = ", self.y, " z = ", self.z)


print("Creating First Object of Parent Class")	
ob1=MyClass(45, 27)
ob1.show()
ob1.showSum()
ob1.showProduct()


print("Creating Second Object of Sub Class 1")	
ob2=MySubClass1(8, 16)
ob2.show()
ob2.showSum()
ob2.showProduct()


print("Creating Third Object of Sub Class 2")
ob3=MySubClass2(11, 54, 39)
ob3.show()
ob3.ThreeSum()
ob3.ThreeProduct()

Productivity

Output showing how to implement inheritance in Python

Source

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Popular Articles