Inheritance and Polymorphism
A class in Java can extend another class. All of the extended class in included automatically in the new class although the private stuff in the extended class are not available in the new class. To extend a class do:
class B extends A {
//
all fields, method and classes of class A are included in class B
//
that which is declared private is not included
//
new fields and methods can be added
// methods
of class A can be overridden (redefined)
}
A is the superclass of B and B is a subclass of A. Some compiler rules for superclass and interface variables are:
A superclass variable can
be assigned a variable/object of any of it's subclasses (not vice
versa)
A superclass variable can be cast to any of it's
subclasses( vice versa is OK)
An interface variable can be
assigned a variable/object of any implementing class
Any type
variable can be cast to any interface
An interface variable<T>
variable can be assigned a variable/object of class T
A variable
of class T can be cast to any interface
Since class Object is the superclass of all classes a superclass variable can be assigned any class variable/object. And a superclass variable can be cast to any class
See Example5.java and Example5A.java
A class can have many subclasses but only one superclass. One of the primary uses of inheritance is reuse of code (as an alternative to cutting and pasting). An example of this is extending the JPanel class as in JButtonsExamplePanelExtension. It is much easier to use the code from the JPanel class than to write code to construct a JPanel. In general is an object of class B is like an object (ISA) of class A then inheritance should be considered. Inheritance also allows us to override class A methods in subclasses. This allows class A variables to be assigned variables/objects of any of it's subclass and have the currently assigned subclass method to be choses automatically. This is called polymorphism. This is shown in the Employee subclasses (see Examples page) where all types of Employees have a pay method but each type is a different calculation. Another example is a collections of shapes (polygons, lines, rectangles, squares, ellipses and circles for example). All of these can be drawn but each is drawn differently. We can have a variable of type shape that knows how to draw itself.
A polymorphic variable can be assigned a variable/object of many different types. If there is a method common to all of the types then the polymorphic variable will call the method of the type assigned at runtime.
Sometimes when constructing a hierarchy of classes there could be many subclasses. A way to create objects of many types but not have so many classes is to do it dynamically using the decorator pattern. The decorator pattern is associated with an interface. To use the decorator pattern with interface AnInterface do:
interface AnInterface
{
// the interface methods go here
// suppose void method1();
represents one of the methods
}
class Decorator implements
AnInterface {
private AnInterface AnInterfaceVar;
public
Decorator(AnInterface x) {
AnInterFaceVar = x;
}
// the
interface methods are implemented by calling them using
AnInterfaceVar
// for example
public void method1()
{
anInterfaceVar.method1();
}
// accessor for AnInterFacVar
should be included
}
See CoffeeShop1 and CoffeeShop2 for examples.