## 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( )**.

**Example**

#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:

**Example**

>>> 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.

**Example**

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.

**Example**

>>> from inheritEllipse import ellipse, circle >>> c1=circle(20) >>> c1.area() 1256.6370614359173 >>> c1.perimeter() 125.66370614359172 > > >