Nav-Bar 

Inheritance 


Java Zone Index Page Search the Java Zone Site Java Dictionary

View the Tutorials Archive

What is Java? Java syntax and general style guide How to get Java running
Links to relevant sites

Email me

Quick guide to Inheritance

The following diagram shows a class called Bob being extended - the new class being called "BigBob". In the "Attributes" box, there is a type followed by the variable name. In the "Method" box there is a return type, followed by a method name, and any parameters are given in brackets. This is elementary though - the diagram is to illustrate inheritance from one class to another.

When a new class extends an old one, the old class is the superclass (or base, or parent), and the new one is the subclass (or derived class or child). All of the public attributes and methods possessed by the superclass will be inherited by the subclass. Notice that the subclass has access to the superclass' methods and attribute, but the superclass doesn't have access to the subclass' methods or attributes.
Also, through polymorphism, wherever a class of type "Bob" is required, a BigBob class will be allowed too. If, however, a BigBob is asked for, because it has more methods and attributes than a standard Bob, only a BigBob is acceptable. Notice that BigBob only directly inherits from one other class. This is because Java only supports single inheritance as opposed to multiple inheritance - only one class may by a class' superclass. (note that a class may be extended by many other classes though - indeed, the class called "Object" is the superclass of every class in Java. Object is the only class that doesn't have a superclass).