Previous | Next | Trail Map | Writing Java Programs | Objects, Classes, and Interfaces


Creating a Subclass

You declare that a class is the subclass of another class within The Class Declaration. For example, suppose that you wanted to create a subclass, ImaginaryNumber, of the Number class. (The Number class is part of the java.lang package and is the base class for Integers, Floats and other numbers.) You would write:
class ImaginaryNumber extends Number {
    . . .
}
This declares that ImaginaryNumber is the subclass of the Number class.

A subclass inherits variables and methods and methods from its superclass. Inheritance is one of object-oriented programming's most powerful paradigms. Through inheritance you can reuse code many times and build a hierarchy of increasingly specialized classes.

Creating a subclass can be as simple as including the extends clause in your class declaration (such as in the declaration in ImaginaryNumber above). However, you usually have to make other provisions in your code when subclassing a class, such as overriding methods.

What Member Variables Does a Subclass Inherit?


Rule:A subclass inherits all of the member variables within its superclass that are accessible to that subclass (unless the the member variable is hidden by the subclass).
That is, subclasses [PENDING: show example]

For more information about which member variables within its superclass a subclass has access to refer to Controlling Access to Member Variables.

If your subclass defines a member variable with the same name as a member variable in its superclass, the variable in the subclass hides the one in the superclass. Thus, the subclass does not inherit the variable from its superclass.

[PENDING: show example]

Hiding Member Variables

As mentioned in the previous section, member variables defined in the subclass hide member variables of the same name in the superclass.

[PENDING: example of why this might be useful].

While this feature of the Java language is powerful and convenient, it can be a fruitful source of errors: hiding a member variable can be done deliberately or by accident. So, when naming your member variables be careful to only hide those member variables that you actually wish to hide.

What Methods Does a Subclass Inherit?

The rule for which methods get inherited by a subclass is similar to that for member variables.
Rule:A subclass inherits all of the methods within its superclass that are accessible to that subclass (unless the method is overriden by the subclass).
That is, subclasses [PENDING: show example]

For more information about which methods within its superclass a subclass has access to refer to Controlling Access to Methods.

If your subclass defines a method with the same name and signature as a method in its superclass, the method in the subclass overrides the one in the superclass. Thus, the subclass does not inherit the method from its superclass.

[PENDING: show example]

Overriding Methods

The ability of a subclass to override a method in its superclass allows a class to inherit from a superclass that is "close enough" in functionality and then supplement or modify the behaviour of that superclass.


Previous | Next | Trail Map | Writing Java Programs | Objects, Classes, and Interfaces