Aqua Phoenix
     >>  Research >>  OOP  
 

Navigator
   
 
       
   

13. Polymorphism

13.1 Summary

  • What is polymorphism?
  • How is polymorphism implemented in Java - Interfaces
  • How is a Mouse Event an Interface?

13.2 Contents

13.2.1 What is polymorphism?

Polymorphism is the ability to redefine methods for subclasses. Specifically this means that some method can be defined for an object, and every derived class of this object redefines the method depending on the type of object. For an example, let's take a fictitious object named "Shape". There exists some method called "getArea()" for this object Shape. We now implement a Shape-derived class by the name of "Circle". We define a method called "getArea()" in "Circle", which returns the area of an instantiated circle. We also create a Shape-derived class by the name of "Square". We again define a method called "getArea()" in "Square", which returns the area of an instantiated square. Should we come across an instantiated Shape, not knowing whether it is a Circle or a Square, we may call the method "getArea()" to retrieve the correct area. We are able to call the method "getArea()" on an unknown Shape object, because both Circle and Square implemented the generically defined methods of the object Shape. This feature is called Polymorphism, and it is essential to all Object Oriented Languages.

13.2.2 How is polymorphism implemented in Java - Interfaces

Even though you may not have noticed, you have already been using polymorphism from the minute you started working with applets. Polymorphism is achieved in Java by the use of Interfaces. ActionListeners and ItemListeners are examples of interfaces. They are included in applets by means of the implements keyword. While any given object can extend at most one superclass (Single inheritance), objects can implement multiple interfaces (Poly-morphism). Let us work out the Shape example from above. We will need 3 files altogether: 1. Shape interface, 2. Circle object, 3. Square object

1.

interface Shape {

  public double getArea();
}
2.

class Circle implements Shape {

  double radius;


  public Circle(int double radius) {

    this.radius = radius;
  }


  public double getArea() {

    return (radius * radius * 3.1415);
  }


  public void donothing() {
  }
}
3.

class Square implements Shape {

  double side;


  public Square(int double side) {

    this.side = side;
  }


  double getArea() {

    return (side * side);
  }
}
Let us analyze a few things about the interface. Besides a different declaration than the ones we have already seen, the only notable aspect is the way methods are listed in the interface. Each method that is common to all objects that implement Shape needs to be listed in the interface. For objects Circle and Square, the method getArea() appears just like in the interface. Implementing an interface does not mean though, that the object is restricted to contain only those methods listed in the interface. Notice the "donothing()" method in object Circle. Besides not doing anything, the method is entirely valid.

Note, that you cannot instantiate an interface. Think about the Shape example; while we can certainly define a circle (radius) or a square (length of a side), we cannot define a shape. What is a shape? It is something that can take the form of a circle or square, but it does not exist by itself. For that very reason, Shape remains an interface to other clearly defined shapes.

Note also, that if you implement some Interface in your object, you will necessarily have to write out all functions that are required by the interface. In our example, if we made a Shape X that didn't have an area, we would still need to place the method "getArea()" in our shape X's class file. Think about why this is! In case this happens, we can leave the method's contents emtpy, or simply return some constant value, like 0. We will see an example of an Interface, in which we do not need all methods, which is why we leave the unnecessary ones empty.

13.2.3 How is a Mouse Event an Interface?