Objects, Classes, and Interfaces |
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 namedSubClass
of another class namedSuperClass
. You would write:This declares thatclass SubClass extends SuperClass { . . . }SubClass
is the subclass of theSuperclass
class. It also implicitly declares thatSuperClass
is the superclass ofSubClass
. A subclass also inherits variables and methods from its superclass's superclass, and so on up the inheritance tree. To simplify our discussion, when this tutorial refers to a class's superclass it means the class's direct ancestor as well as all of its ascendant classes.A Java class can have only one direct superclass. Java does not support multiple inheritance.
Creating a subclass can be as simple as including the
extends
clause in your class declaration. However, you usually have to make other provisions in your code when subclassing a class, such as overriding methods or providing implementation for abstract 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 member variable is hidden by the subclass).
The following list itemizes the member variables that are inherited by a subclass:
- Subclasses inherit those member variables declared as
public
orprotected
.- Subclasses inherit those member variables declared with no access specifier as long as the subclass is in the same package as the superclass.
- Subclasses don't inherit a superclass's member variable if the subclass declares a member variable using the same name. The subclass's member variable is said to hide the member variable in the superclass.
- Subclasses don't inherit the superclass's
private
member variables.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.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.
One interesting feature of Java member variables is that a class can access a hidden member variable through its superclass. Consider this superclass and subclass pair:
Theclass Super { Number aNumber; } class Sub extends Super { Float aNumber; }aNumber
variable in Sub hidesaNumber
inSuper
. But you can accessaNumber
from the superclass with:super.aNumber
super
is a Java language keyword that allows a method to refer to hidden variables and overriden methods of the superclass.What Methods Does a Subclass Inherit?
The rule that specifies which methods get inherited by a subclass is similar to that for member variables.A subclass can either completely override the implementation for an inherited method, or the subclass can enhance the method by adding functionality to it.
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).
The following list itemizes the methods that are inherited by a subclass:
- Subclasses inherit those methods declared as
public
orprotected
.- Subclasses inherit those superclass methods declared with no access specifier as long as the subclass is in the same package as the superclass.
- Subclasses don't inherit a superclass's method if the subclass declares a method using the same name. The method in the subclass is said to override the one in the superclass.
- Subclasses don't inherit the superclass's
private
methods.Overriding Methods
The ability of a subclass to override a method in its superclass allows a class to inherit from a superclass whose behavior is "close enough" and then supplement or modify the behavior of that superclass.
Objects, Classes, and Interfaces |