Polymorphism Code Examples in Java
Polymorphism
refers to a feature of Java that allows an object of a
superclass to refer to an object of
any subclass.
This is possible because all objects of a base class are also objects of its
superclass. For
example,
assume that all circles are represented by a class called Circle,
which is a subclass of the
class Shape
(which thus is the superclass of Circle).
Further assume that Triangle is
another subclass of Shape.
Now assume that Shape has
a method draw().
You can implement these classes in such a way
that when you invoke the draw() method
on a Shape variable (referring to an object of Triangle),
it draws a triangle, and when you invoke the same method on the same Shape
variable
(which now is
referring to an object of Circle),
it draws a circle. It is the same method of the Shape
class with
different implementations in the subclasses.
So,
polymorphism helps to prevent the code from becoming more complex and
cumbersome,
by allowing
you to make different implementations of related functionalities (behaviors)
under one
name: object or method.
Example Code:
package
aurangzeb.utk.com;
public class Shape {
public void draw(){
System.out.println("Draw in
Shape");
}
public void drawIn3D(){
System.out.println("Draw 3D
in Shape");
}
public void methodInShap(){
System.out.println("Just A
method In Shape");
}
}
package
aurangzeb.utk.com;
public class Circle extends Shape{
public void draw(){
System.out.println("Draw in
Circle");
}
public String
drawIn3D(int dim){
return "1";
}
public String drawIn3D(int dim,int x , int y ){
return "0";
}
public void
methodInCircle(){
System.out.println("Just A
method In Circle");
}
}
package
aurangzeb.utk.com;
public class Client {
/**
* @param args
*/
public static void main(String[]
args) {
// TODO
Auto-generated method stub
Shape
shape = new Shape();
Circle cricle = new Circle();
shape.draw();
cricle.draw();
Shape shape1 = new Circle();
shape1.draw();
cricle=(Circle)shape1;
cricle.draw();
shape=cricle;
shape.draw();
}
}
Dynamic Method Dispatch:
- A mechanism by which a call to an overridden method is resolved at run time, rather than compile time
-
As you know, a superclassreference variable can refer to a subclass object.
- When an overridden method is calledthrough a superclassreference
- Java determines which version of method to execute based upon the type of object being referred toat the time call occurs !
- It is type of objectbeing referred to that determineswhich versionof an overriddenmethod will be executed
Another Code Example Dynamic Method Dispatch
package
aurangzeb.utk.com;
public class A {
public void callMe(){
System.out.println("I am in
Class A");
}
}
package
aurangzeb.utk.com;
public class B extends A{
public void callMe() {
System.out.println("I am in
Class B");
}
}
package
aurangzeb.utk.com;
public class C extends A{
public void callMe() {
System.out.println("I am in
Class C");
}
}
package
aurangzeb.utk.com;
public class
DynamicDispatchDemo {
/**
* @param args
*/
public static void main(String[]
args) {
// TODO
Auto-generated method stub
A
a = new A(); // Object of
type A
B
b = new B(); // Object of
type B
C
c = new C(); // Object of
type C
A
r; // reference of Type A
r=a;
r.callMe();
//
call A version
r=b;
r.callMe();
//
call B version
r=c;
r.callMe();
//
call C version
}
}
