Python Data Persistence – Overriding
With two similar radii for the circle, the equation of the area of the ellipse (π*r1 *r2) turns out to be 7rr2, and the equation of perimeter of the ellipse (2π effectively becomes 27πr. However, we would like to redefine area( ) and perimeter( ) methods, which are inherited by circle class, to implement these specific formulae. Such redefinition of inherited methods is called method overriding.
Modify circle class as per following code:
Example
class circle(ellipse): def___init___(self, r1, r2=None): super().__init___(r1,r2) self.radius2=self.radiusl def area(self) : area=math.pi*pow(self.radius1,2) return area def perimeter (self) : perimeter=2*math.pi*self.radius1 return perimeter
The result will be unaffected though. Python class can be inherited from multiple classes by putting names of more than one class in parentheses of the class definition.
Protected?
As mentioned earlier, Python doesn’t believe in restricting access to attributes. To emulate the behavior of Java-like protected access specifier, a single underscore character is prefixed to the instance attribute. However, for all practical purposes, it behaves like a public attribute, not even requiring name mangling syntax as in the case of the ‘private’ attribute (prefixed by double underscores). The _ character serves merely as a deterrent expecting responsible programmer to refrain from using it outside inherited class.