Inheritance

Inheritance is the property whereby one class extends another class by including additional methods and/or variables. The original class is called the superclass of the extending class, and the extending class is called the subclass of the class that is extended. Since a subclass contains all of the data and methods of the superclass plus additional resources, it is more specific; conversely, since the superclass lacks some of the resources of the subclass, it is more general or abstract, albeit less detailed, than its subclasses.

Inheritance. Shows the superclass of the class shown below.


Inheritance. Shows a subclass of the class shown above. Additional variables and methods extend the original class.


Inheritance. In many contexts, the class and its superclass can be viewed as the union of their variables and methods.

When viewed as a whole, the classes related to one another through the subclass - superclass relationship form a hierarchy. This hierarchy is called the class hierarchy. It is necessary to keep the class hierarchy in mind when extending classes.

Class hiearchy. The classes related to one another through the class - superclass relationship form a class hierarchy.

The current Java class hierarchy, as defined by Sun, can be browsed through the Java API.