12.5 Overriding
Overriding is the process of replacing an existing method in an inherited class with one more suitable for the new class. In the point
and point3D
examples appearing in the previous section, the distance
method (presumably) computes the distance from the origin to the specified point. For a point on a two-dimensional plane, you can compute the distance using the following function:
d = √ x2 + y2 |
However, the distance for a point in 3D space is given by this equation:
d = √ x2 + y2 + z2 |
Clearly, if you call the distance
function for point
for a point3D
object, you will get an incorrect answer. In the previous section, however, you saw that the P3
object calls the distance
function inherited from the point
class. Therefore, this would ...
Get The Art of Assembly Language, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.