Aqua Phoenix
     >>  Lectures >>  Java 3  
 

Navigator
   
 
       
   

3.2 Polymorphism - Interfaces

Polymorphism is a feature in Object Oriented Programming by which a class implements a particularly named method, and fits its purpose to its needs. This is not too different from Inheritance, in which methods from a super-class are re-written in a sub-class in order to define the bahavior of the sub-class. However, there are differences, and they outlined below.

The purpose of polymorphism is for classes to extend their functionality by implementing commonly defined methods. This makes most sense for a group of classes that have some feature in common, and could be considered "similar" with respect to that feature.

For example, squares, circles, and triangles have common properties: they all have an area and a circumference. Using Inheritance, we define a super-class by the name of Shape, and each class of Circle, Square, and Triangle extends the superclass, and re-defines common methods to compute area and circumference for the particular shape. After extending Circle, Square, and Triangle to class Shape, these three classes can now be referred to, and cast as class Shape.

In polymorphism, the classes Circle, Square, and Triangle implement an interface which similarly defines the methods for computing area and circumference. The classes which implement the interface Shape must necessarily contain the method names for ALL methods declared by the interface. After implementing an interface, the class can also be referred to and cast as the common interface name.

An interface for Shape may, for example, declare the following methods:

public double getArea();
public double getCircumference();
For class Circle to extend the interface Shape, the following changes in class Circle are required:

public class Circle implements Shape {

  public Circle() {
  }

  ...

  public double getArea() {

    // compute area and return
  }

  public double getCircumference() {

    // compute circumference and return
  }
}
Before implementing interface Shape, an object of type Circle could only be cast as a Circle:

Object myCircle = new Circle(...);
Circle circle = (Circle)myCircle;
After implementing interface Shape, the object can now also be cast as a Shape:

Object myCircle = new Circle(...);
Circle circle = (Circle)myCircle;
Shape shape = (Shape)myCircle;
There are several benefits to specifically declaring common methods in an interface, and then defining them inside classes. Finding common functionality between classes and abstractly grouping it allows us to refer to objects in different ways.

For the example of shapes, if Circle, Square, and Triangle all implement interface Shape, then they can be stored in one array of type Shape:

Shape[] shapes = new Shape[10];
shapes[0] = new Circle(...);
shapes[1] = new Square(...);
shapes[2] = new Triangle(...);
...
for (int i = 0; i < shapes.length; i++) {
  shapes[i].getArea();
}
While this type of grouping for commonality can be achieved using Inheritance as well, there are cases where Polymorphism is more applicable. See comparison below.

3.2.1 Writing an Interface

An interface is merely a declaration of methods. A class that implements the interface is then obligated to include ALL of the methods from the interface. If for some reason one or the other method does not apply, then it suffices to include the method prototype without including any code for it.

For the above example, interface Shape is defined as follows:

public interface Shape {
  public double getArea();
  public double getCircumference();
}
Note that there is no code included for the methods. An interface serves only as a defintion of which methods must be included and what the method prototypes look like.

3.2.2 Comparison: Inheritance vs. Polymorphism

  Inheritance Polymorphism
Keyword extends implements
Proper name of "parent" Superclass Interface
Semantics A class is said to extend a superclass when it takes the superclass' functionality (its methods and variables) and adapts it to its own needs. A Circle extends the abstract term of Shape by giving it a unique way of computing area and circumference. A Rectangle does the same, but area and circumference is computed differently. A class is said to implement an interface when it takes the interface's method prototypes and fills them with code. By itself, an interface has no functionality, because none of the methods in an interface contain code. They are merely prototypes (templates). The implementing class then takes the method prototypes and assigns actual code to them.
Commonality / What is shared? Strict inheritance of superclass' protected methods AND variables. Inherits ONLY method definitions (prototypes). Variables are not inherited, because variables or constants cannot be declared in an interface.
What must be implemented? Nothing to Everything. When a subclass extends a superclass, it can re-define any of the methods and variables. A circle's area is computed differently from a square's area, so it makes sense to re-define each class' getArea() method to compute area the right way. Everything. All methods that are defined in the interface must appear in the class that implements that interface. Methods that are not implemented (i.e. not applicable and not used) are included only as prototypes (i.e. method definitions)
Type of Relationship Subclass is a kind of the superclass. E.g. An apple is a kind of fruit. A student is a kind of human. The class implementing the interface does not have to be semantically common to the superclass. While it usually is, there are many interfaces that are used to implement very generic methods, e.g. a MouseListener. The MouseListener can be added to any graphical object to capture mouse interaction, but the graphical objects must not have any commonality.
Inheritance Model Single. A class can only extend ONE superclass. Multiple inheritance does not exist in Java. Multiple. A class can implement many interfaces.