Monday, July 9, 2007

Polymorphism

Polymorphism
• A variable of a superclass type can reference an object of a subclass type.
Computer myComp, yourComp;
// what exactly is happening in RAM at this point?
myComp = new Computer(arg list);
yourComp = new Laptop(arg list);
• Polymorphism means many forms or many shapes
• Polymorphism allows the JVM to determine which method to invoke at run time
• At compile time, the Java compiler can’t always determine what type of object a superclass may reference but it will be known at run time

Example of polymorphism
• Suppose we declare an object of type Computer
• Then we ask the user if they have a desktop or laptop
• If it is a desktop, we use the Computer constructor
• If it is a laptop, we use the Laptop constructor.
• Only when a constructor is called is space set aside on the heap to store the information for the object.
• Until execution, the actual dynamic type is unknown. This is called late binding.

Using polymorphism
• Suppose we declare an arrays of Computers,
– then fill the array with computers in a certain lab,
– some of which are laptops, some desktops
Computer [ ] labComp = new Computer[10]
• Then when we loop through the array
– we can call toString for each computer in the array, and the method for the type will be called

Abstract classes & methods
• Sometimes you want to require that all subclasses override a method
– Example: a print method, where what is to be printed varies among sublclasses
• This is, in essence, requiring all subclasses to implement behavior that is only being specified (not implemented) in the superclass.
• The abstract keyword is used in this case
public abstract void print( );
Classes can be abstract, as well as methods.

Polymorphism: another look
• Polymorphism means that there are a number of different subclasses of objects that will respond to the same message
• Example: Shape superclass
• and Circle, Square, Triangle subclasses that inherit all the methods and data members of Shape
• One of the things we would like to do with all our Shape classes is to find the area of each
• So in class Shape we want a method findArea( ) to be sure that all the subclasses override it.
– But what is the formula to find the area of a shape?
– Since we don’t know, we cannot implement the method findArea( )
– This makes it an Abstract method: i.e. no implementation
– Any class that contains an abstract method is an abstract class.

Declaring abstract methods or classes
• public abstract class Shape
{ private double side; // data field

public abstract double findArea( );
// note semicolon & no implementation
} // end class Shape


Declaring objects of a subclass
• An object of type Circle, Square, or Triangle is also, by inheritance, of type Shape
• In main, where we instantiate object to use the Shape classes, we could declare three object of type shape
Shape cir, squar, tri;
• Then to instantiate each object, we must call their constructors
cir = new Circle(args);
squar = new Square(args);
tri = new Triangle( args);
• Now we could declare an array of shapes, some of which are circles, some triangles, and some squares
– When we loop through the array, we call findArea( ) for each object, and automatically, the correct findArea( ) method is called for each shape.
• This, then is polymorphism: one call to findArea( ) results in different behavior depending on the type of each object.

Interfaces in Java
• A Java subclass can be derived from only one superclass (no multiple inheritance)
• Sometimes you would like to have several classes have a common interface (i.e. implement all the same methods), but also inherit from a superclass.
– This is an alternative to multiple inheritance
• An interface is similar to a class definition in that it includes the definition of methods
– But an interface cannot be instantiated
• You can derive a class from an interface, and if you implement ALL the required methods, you can instantiate that class.

Concrete & Abstract classes
• An abstract class is one that has at least one abstract method
– It is provided only to have other classes derived from it
– An object of this class cannot be instantiated
• A concrete class is has no abstract methods
– If you declare a subclass of an abstract class, and do not implement its abstract methods, you will get a compiler error

Abstract Classes and Interfaces
• Abstract classes are much like Interfaces, in that they both may have methods that are not implemented.
• One difference is that some of the methods of an abstract class can be implemented,
– while no methods of an Interface can be implemented
• Another difference is that abstract classes can define instance variables
– Interfaces cannot do this.

Abstract Class Number and the Java Wrapper Classes
• The abstract class Number has as its subclass all the wrapper classes for primitive numeric types
• A wrapper class is used to store a primitive-type value in an object type
• Each wrapper class contains constructors to create an object that stores a particular primitive value
• A wrapper class has methods for converting the value stored in a different numeric type
• Look at the Java API for the Number class

No comments: