Inheritance and Polymorphism

The Circle class builds on the Point class using inheritance. The Circle class adds one additional data attribute (radius), and three new methods. It also reimplements a few of Point's methods. Here is the complete class definition:

class Circle(Point):

self.radius = radius def edge_distance_from_origin(self):

return abs(self.distance_from_origin() - self.radius)

def area(self):

def circumference(self):

return 2 * math.pi * self.radius def _eq_(self, other):

return self.radius == other.radius and super()._eq_(other)

return "Circle({0.radius!r}, {0.x!r}, {0.y!r})".format(self)

return repr(self)

Inheritance is achieved simply by listing the class (or classes) that we want our class to inherit in the class line.* Here we have inherited the Point class—the inheritance hierarchy for Circle is shown in Figure 6.3.

object

new (

)

init

()

_eq_()

_repr__

()

str (

)

Key inherited implemented

+1 0

Post a comment