Python Data Persistence – Inheritance
Of all features of object-oriented programming methodology, inheritance is arguably the most important, and hence the most used feature in software development. Leaving aside its literary meaning, inheritance in the context of programming refers to the mechanism by which features of one class (attributes and methods) are made available to another class that has the option to define additional resources or modify the functionality of inherited methods.
Python makes extensive use of inheritance. As we have seen, each class is inherited from the built-in object class. Inheritance enables a template definition of properties in a parent class to be implemented by its subclass for a more specific purpose.
One such case in point is the relationship between built-in classes ‘int’ and ‘bool’. Inheritance comes into the picture whenever there is ‘IS A’ kind of relationship between classes. Here a ‘bool’ object IS A(n) ‘int’ right? Hence ‘int’ class is a superclass of the ‘bool’ class. As you know a bool object has only two possible values True and False. They are equivalent to 1 and 0 respectively which are integers by the way.
Let us establish the inheritance of two customized classes with the following example. Ellipse an elongated circle with two radii and one radius is larger than the other. The circle can be seen as a more specific case of ellipse where two radii are equal. So circle IS an A(n) ellipse! We are going to define an ellipse class with two instance attributes and two methods area( ) and perimeter( ).
- Basic Java Interview Questions in Java
- Top 100+ Core Java Interview Questions and Answers | Java Interview Questions for Freshers and Experienced Professionals
- Python Data Persistence – class Keyword
#inheritEllipse.py import math class ellipse: def__init__(self, r1, r2): self.radius1=r1 self.radius2=r2 def area(self): area=math.pi*self.radiusl*self.radius2 return area def perimeter(self): perimeter=2 *math.pi*math. sqrt((pow(self.radius1,2)+pow(self.radius2,2))/2) return perimeter
Note that formula for area of ellipse = π*rl *r2 and perimeter of ellipse =2π
Let us import this class in the interpreter and declare objects as follows:
>>> from inheritEllipse import ellipse >>> e1=ellipse (20,30) >>> e1.area() 1884.9555921538758 >>> e1.perimeter() 160.19042244414092 >>> e1=ellipse(20,20) >>> e1.area() 1256.6370614359173 >>> e1.perimeter() 125.66370614359172
Note that in the second case both radii are equal, hence the ellipse happens to be a circle. We now design a circle class using the ellipse class as its parent. Add following lines in inheritEllipse .py code.
class circle(ellipse) : def__init___(self, r1, r2=None): super ( ) .__init__(r1 , r2) self.radius2=self.radius1
Just import this inherited circle class, declare an object with a radius of 20. The result shows that the perimeter and area match that of the ellipse object with identical radii.
>>> from inheritEllipse import ellipse, circle >>> c1=circle(20) >>> c1.area() 1256.6370614359173 >>> c1.perimeter() 125.66370614359172 > > >