The derived class is called a subclass. The class from which its derived is called the superclass.
In Java all classes must be derived from some class. The top-most class, the class from which all other classes are derived, is the Object class defined in java.lang:
A subclass is a class that derives from another class. It subclass inherits state and behavior in the form of variables and methods from all of its ancestors. It can just use the items inherited from its superclass as is, or the subclass can modify or override it. So, as you drop down in the hierarchy, the classes become more and more specialized.
To declare a subclass you would write:
class SubClass extends SuperClass { . . . }
![]() | Caution |
---|---|
![]() A Java class can have only one direct superclass. Java does not support multiple inheritance. |
A subclass can either completely override the implementation for an inherited method or the subclass can enhance the method by adding functionality to it.
Replacing a Superclass's Method Implementation
Adding to a Superclass's Method Implementation
Methods a Subclass Cannot Override
class ChessAlgorithm { . . . final void nextMove(ChessPiece piece) {...} static void mate(ChessPiece piece) {...} . . . }
Methods a Subclass Must Override
abstract class GraphicObject { . . . abstract void draw(); . . . }
You can declare that your class is final; that is, that your class cannot be subclassed. There are (at least) two reasons why you might want to do this: security reasons and design reasons.
Classes, which implement abstract concepts and should not be instantiated, are called abstract classes. An abstract class is a class that can only be subclassed - it cannot be instantiated.
An abstract class may contain abstract methods, that is, methods with no implementation. In this way, an abstract class can define a complete programming interface thereby providing its subclasses with the method declarations for all of the methods necessary to implement that programming interface.
Abstract classes provide a way to represent objects at a conceptual generic level.
In a graphic library, you can create an abstract class to represent a generic graphic object and a method for drawing it. This method is them only implemented on the derived classes.
Copyright © 1998-2009 Dilvan Moreira