Professional Documents
Culture Documents
Chapter Five
Polymorphism
public Point3()
{
}
1
Object Oriented Programming with Java
2
Object Oriented Programming with Java
import javax.swing.*;
public class PolyTest1{
public static void main(String args[])
{
Point3 point = new Point3(30,50);
Circle4 circle = new Circle4(120,89,2.7);
String output = “Call Point3’s toString with superclass”+
“reference to superclass object:\n”+ point.toString();
}//end class
- In the above program, the statement Point3 pointRef = circle; assigns the
reference of subclass object circle to superclass-type variable pointRef. A
superclass-type variable that contains a reference to a subclass object calls the
subclass method; hence, pointRef.toString() actually calls class Circle4’s toString
3
Object Oriented Programming with Java
4
Object Oriented Programming with Java
- In the above code, we have such an assignment: point = circle; Java compiler
allows this because a Circle4 is a Point3 object. Using point reference, we can
only invoke superclass only methods. But when we try to invoke subclass only
methods using point, the Java compiler generates error on each of these lines,
because these are not methods of the superclass Point3. (in this case, using a
Point3 variable, we can invoke only Point3 methods getX, getY, setX, setY
and toString)
- An interface is like a class that can’t be instantiated. Its purpose is to specify a set
of requirements for a class to implement. These requirements are regarded as a
“contract” between the implementing class and any client class that uses it.
- An interface is a description of a capability. It lists the methods that a class must
implement to carry out that capability.
- An interface typically consists of declarations of related methods, although an
interface also may contain static and final fields that represent constants. In any
case, all interface members are public. A class that implements an interface
typically provides definitions for the methods declared in the interface.
5
Object Oriented Programming with Java
- A class that implements an interface must provide an implementation for all the
method signatures in the interface. An interface declaration introduces a new
reference type whose members are constants and abstract methods.
- An interface is typically used in place of an abstract class when there is no default
implementation to inherit. i.e., no instance variables and no default methods
implementations.
- A compile-time error occurs if the identifier naming an interface appears as the
name of any other class or interface in the same package.
- All interface methods are abstract regardless of whether the term abstract
occurs. In more basic terms, an interface is abstract because an interface can’t be
instantiated as an object.
- Let us see an example here:
public interface Shape{
public double getArea();
public double getVolume();
public String getName();
}//end interface
6
Object Oriented Programming with Java
Use the Circle4 program and the following Cylinder class for the coming illustration
7
Object Oriented Programming with Java
import javax.swing.*;
import java.text.*;
arrayOfShape[0] = point;
arrayOfShape[1] = circle;
arrayOfShape[2] = cylinder;
8
Object Oriented Programming with Java
arrayOfShapes[i].toString()+ “\nArea =
”+twoDigits.format(arrayOfShapes[i].getArea())+ “\nVolume = ”+
twoDigits.format(arrayOfShapes[i].getVolume()); }
JOptionPane.showMessageDialog(null,output);
System.exit(0);
}
}//end class
Multiple inheritances
- A class can be derived from more than one superclass. This concept is called
multiple inheritance. But such a notion can’t be directly used in Java. Therefore,
one has to use an interface if a class is expected to inherit behaviors from more
than one superclass. For instance, Let us see the declaration of class Point:
- In Java, all classes inherit from class Object. So whether we write the statement
“Point extends Object” or not, the Java compiler processes the inheritance
relationship among Point and Object classes. Therefore, our class Point will have
both the functionality of class Object and that of Shape, hence multiple
inheritance. The point to be underlined here is, multiple inheritance is only
possible through the use of interfaces.
- To summarize the discussion of interface; lets see the following comparison.
- Interface are very useful for specifying what a class should be able to do, while
deferring to a later time how the contract should be implemented. But in some
cases, interfaces are too restrictive. They allow no variable definitions (except
public final constants) or executable statements. Often, it is better to proceed with
a partial class implementation, deferring only those methods that depend directly
upon the backing data structure. That is when an abstract class should be used.