You should now understand polymorphism, and how to apply it. You will find that this technique can be utilized to considerable advantage in the majority of your Java programs. It will certainly appear in many of the examples in the remaining chapters.
The important points we have covered in this chapter are:
An abstract method is a method that has no body defined for it, and is declared using the keyword abstract.
An abstract class is a class that contains one or more abstract methods. It must be defined with the attribute abstract.
You can define one class based on another. This is called class derivation or inheritance. The base class is called a superclass and the derived class is called a subclass. A superclass can also be a subclass of another superclass.
A subclass inherits certain members of its superclass. An inherited member of a class can be referenced and used as though it was declared as a normal member of the class.
A subclass does not inherit the superclass constructors.
The private members of a superclass are not inherited in a subclass. If the subclass is not in the same package as the superclass, then members of the superclass that do not have an access attribute are not inherited.
The first statement in the body of a constructor for a subclass should call a constructor for the superclass. If it does not, the compiler will insert a call for the default constructor for the superclass.
A subclass can re-implement, or overload, the methods inherited from its superclass. If two or more subclasses, with a common base class, re-implement a common set of methods, these methods can be selected for execution at run-time.
A variable of a superclass can point to an object of any of its subclasses. Such a variable can then be used to execute the subclass methods inherited from the superclass.
A subclass of an abstract class must also be declared as abstract if it does not provide definitions for all of the abstract methods inherited from its superclass.
A class defined inside another class is called a nested class or inner class. An inner class may itself contain inner classes.
An interface can contain constants, abstract methods, and inner classes.
A class can implement one or more interfaces by declaring them in the class definition, and including the code to implement each of the interface methods.
If several classes implement a common interface, the methods declared as members of the interface can be executed polymorphically.