Subclasses, Superclasses, and Inheritance |
| INTERMEDIATE |
A class can extend another class. For example, if you have a class named
A, you can say:
class B extends A {
...
}
We say that B is a subclass of A, or
that A is a superclass of B.
What this means is that all the methods and variables declared in the superclass are inherited by the subclass, and can be used in the subclass just as though they were declared there.
Variables and methods that are declared privatein a superclass are inherited by object of the subclass, but are not accessible in the subclass. It is beyond the scope of these pages to explain just what this means.
| STYLE |
You should only extend a class if objects in the subclass are specializations
of the superclass. For example, you could say , because a poodle is a kind of dog. However, you should
not say (or the reverse),
because a person is not a kind of family, nor is a family a kind of person.