Documente Academic
Documente Profesional
Documente Cultură
Polymorphism
Polymorphism allows programmers to send the same message to objects from different classes. i.e., in
its simplest form, polymorphism allows a single variable to refer to objects from different classes.
Polymorphism enables us to “program in the general” rather than “program in the specific”. In
particular, polymorphism enables us to write programs that process objects of classes that are part of
the same class hierarchy as if they are all objects of their classes.
With polymorphism, it is possible to design and implement systems that are easily extensible. New
classes can be added with little or no modification to the generic portions of the program, as long as
those classes are part of the inheritance hierarchy that the program processes generically. The only parts
of a program that must be altered to accommodate new classes are those program components that
require direct knowledge of the new classes that the programmer adds to the
hierarchy.
1
}
public double getDiameter()
{
return 2*getRadius();
}
public double getCircumfrence()
{
return Math.PI*getDiameter();
}
public double getArea()
{
return Math.PI*getRadius()*getRadius();
}
public String toString()
{
return “Center = “+super.toString()+ “; Radius=”+getRadius();
}
}//end class
import javax.swing.*;
public class PolyTest1{
public static void main(String args[])
{
Point point = new Point(30,50);
Circle circle = new Circle(120,89,2.7);
String output = “Call Point’s toString with superclass”+“reference to superclass object:\n”+
point.toString();
output+= “\n\nCall Circle’s toString with subclass ”+“reference to subclass object:\n”+circle.toString();
Point pointRef = circle;
Output+= “\n\ncall Circle’s toString with superclass ”+“ refernce to subclass object:\n
“+pointRef.toString();
JOptionPane.showMessageDialog(null,output);
System.exit(0);
}
}//end class
In the above program, the statement Point 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 Circle’s
toString method. The Java compiler allows this “crossover” because an object of a subclass is an object
of its superclass.
2
{
Point3 point = new Point3(30,50);
Circle4 circle;//subclass-type variable
//assign superclass reference to subclass-type variable
circle = point;
}
}//end class
If you run this program, it will generate an error. Because the assignment is not allowed (incompatible
type). The Java compiler prevents this assignment, because a Point3 is not a Circle4- the “is-a”
relationship applies only between the subclass and its superclass.
It turn out that the Java compiler does allow this assignment if we explicitly cast the superclass
reference to the subclass type. In programs that process superclass and subclass objects by using
superclass reference, only methods of the superclass can be invoked via the superclass reference.
Casting superclass reference to subclass reference (also known as downcasting) enables a program to
invoke subclass functionality to perform subclass-specific operations on subclass objects.
N.B. assigning the reference of a superclass object to a subclass-type variable
(without an explicit cast) is a compile-time error
3
toString)
We have discussed four ways to assign superclass and subclass references to variables of superclass
and subclass types:
1. Assigning a superclass reference to a superclass-type variable is straight forward
2. Assigning a subclass reference to a subclass-type variable is straight forward.
3. Assigning a subclass object’s reference to a superclass type variable is safe, because
the subclass object is an object of its superclass. However, this reference can be used to invoke only
superclass methods. If this code refers to subclass-only members through the superclass variable,
the compiler generates an error.
4. Attempting to assign a superclass object’s reference to a subclass type variable is a
compile-time error. To avoid the error, the superclass reference must be cast to a
subclass type explicitly.
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.
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.
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.
4
Let us see an example here:
public interface Shape{
public double getArea();
public double getVolume();
public String getName();
}//end interface
public class Point extends Object implements Shape{
private int x;
private int y;
public Point(int xValue,int yValue)
{
x = xValue;
y = yValue;
}
public void setX(int xValue)
{
x=xValue;
}
public void setY(int yValue)
{
y=yValue;
}
public int getX()
{
return x;
}
5
return “[”+getX()+ “, ”+getY()+ “]”;
}
}//end class
Use the Circle4 program and the following Cylinder class for the coming illustration
public class Cylinder extends Circle4{
private double height;
public Cylinder(int x,int y,double radius,double heightValue)
{
super(x,y,radius);
setHeight(heightValue);
}
public void setHeight(double heightValue)
{
height = (heightValue <0.0?0.0:heightValue);
}
public double getHeight()
{
return height;
}
public double getArea()
{
return 2 * super.getArea() + getCircumfrence() * getHeight();
}
public double getVolume()
{
return super.getArea() * getHeight();
}
public String getName()
{
return “Cylinder”;
}
public String toString()
{
return super.toString()+ “; Height = ” + getHeight();
}
}//end class
import javax.swing.*;
import java.text.*;
public class InterfaceTest{
public static void main(String args[])
{
DecimalFormat twoDigits = new DecimalFormat(“0.00”);
Point point =new Point(7,11);
Circle4 circle = new Circle4(22,8,3.5);
Cylinder cylinder = new Cylinder(20,30,3.3.3,10.75);
6
String output = point.getName()+ “:”+point+ “\n”+ circle.getName() +
“:”+circle+ “\n”+ cylinder.getName() + “:”+cylinder+ “\n”;
Shape arrayOfShapes[] = new Shape[3];//create shape array
arrayOfShape[0] = point;
arrayOfShape[1] = circle;
arrayOfShape[2] = cylinder;
for(int i=0; i<arrayOfShapes.length;i++)
{
output += ”\n\n ”+arrayOfShapes[i].getName()+ “:” +
arrayOfShapes[i].toString()+ “\nArea =
”+twoDigits.format(arrayOfShapes[i].getArea())+ “\nVolume = ”+
twoDigits.format(arrayOfShapes[i].getVolume());
}
JOptionPane.showMessageDialog(null,output);
System.exit(0);
}
}//end class
When a class implements an interface, the same “is-a” relationship provided by inheritance applies.
For example, class Point implements Shape. Therefore, a Point object is a Shape. In fact, objects of
any class that extends Point are also Shape object.
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:
public class Point extends Object implements Shape{
}//end class
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.
Unlike a class, an interface:
Declares only method headers and public constants;
Has no constructors
Can’t be instantiated
Can be implements by a class
Can’t be implemented by an interface
Can’t extends a class
Can extend several other interfaces.
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.