Ch5 Polymorphism
Ch5 Polymorphism
CHAPTER 5 : POLYMORPHISM
Introduction
Polymorphism is the ability of an object to take on many forms. In other words, polymorphism
allows us to define one interface and have multiple implementations.
Polymorphism can be elaborated as:
An operation may exhibit different behaviour in different instances.
The behaviour depends on the types of data used in the operation.
It plays an important role in allowing objects having different internal structures to share
the same external interface.
Polymorphism is extensively used in implementing inheritance.
For instance, even if we have a variable with the type of a parent class, we can assign it to a child
class and we can call overridden methods in the child class using that variable.
Let's see an example.
class Fruit {
public void show() {
System.out.println("Fruit");
}
}
}
}
class Shape {
void draw() { }
}
void draw() {
System.out.println("Drawing circle (" + x + ", "+ y + ", " + r + ")");
}
}
void draw() {
System.out.println("Drawing rectangle (" + x + ", "+ y + ", " + w + "," + h + ")");
}
}
class ShapesMain {
public static void main(String[] args) {
Shape[] shapes = { new Circle(10, 20, 30),
new Rectangle(20, 30, 40, 50) };
for (int i = 0; i < shapes.length; i++)
shapes[i].draw();
}
}
The declaration of the shapes array demonstrates upcasting. The Circle and Rectangle references
are stored in shapes[0] and shapes[1] and are upcast to typeShape. Each
of shapes[0] and shapes[1] is regarded as a Shape instance: shapes[0] isn't regarded as
a Circle; shapes[1] isn't regarded as a Rectangle.
Late binding is demonstrated by the shapes[i].draw(); expression. When i equals 0, the compiler-
generated instruction causes Circle's draw() method to be called. When i equals 1, however, this
instruction causes Rectangle's draw() method to be called. This is the essence of subtype
polymorphism.
class DisplayOverloading {
public void disp(char c) {
System.out.println(c);
}
public void disp(char c, int num) {
System.out.println(c + " "+num);
}
}
class Sample {
public static void main(String args[]) {
DisplayOverloading obj = new DisplayOverloading();
obj.disp('a');
obj.disp('a',10);
Output:
}
a
}
a 10
In the above example – method disp() has been overloaded based on the number of arguments.
We have two definition of method disp(), one with one argument and another with two arguments.
class DisplayOverloading2{
public void disp(char c) {
System.out.println(c);
}
public void disp(int c) {
System.out.println(c);
}
}
class Sample2{
public static void main(String args[]) {
DisplayOverloading2 obj = new DisplayOverloading2();
obj.disp('a');
obj.disp(5);
Output:
}
a
} 5
class DisplayOverloading3 {
public void disp(char c, int num) {
System.out.println("I’m the first definition of method disp");
}
public void disp(int num, char c) {
System.out.println("I’m the second definition of method disp" );
}
}
class Sample3{
public static void main(String args[]) {
DisplayOverloading3 obj = new DisplayOverloading3();
obj.disp('x', 51 );
obj.disp(52, 'y'); Output:
} I’m the first definition of method disp
} I’m the second definition of method disp
class DemoOverload{
public int add(int x, int y){ return x+y; }
public int add(int x, int y, int z){ return x+y+z; }
public int add(double x, int y){ return (int)x+y; }
public int add(int x, double y){ return x+(int)y; }
}
class OverloadingMain{
public static void main(String[] args){
DemoOverload demo=new DemoOverload();
System.out.println(demo.add(2,3)); //method 1 called
System.out.println(demo.add(2,3,4)); //method 2 called
System.out.println(demo.add(2,3.4)); //method 4 called
System.out.println(demo.add(2.5,3)); //method 3 called
}
}
Answer: It will throw a compilation error: More than one method with same name and
argument list cannot be defined in a same class.
Question 2 – return type is different. Method name & argument list same.
class Demo2 {
public double myMethod(int num1, int num2) {
System.out.println("First myMethod of class Demo");
return num1+num2;
}
public int myMethod(int var1, int var2) {
System.out.println("Second myMethod of class Demo");
return var1-var2;
}}
class Sample5 {
public static void main(String args[]) {
Demo2 obj2= new Demo2();
obj2.myMethod(10,10);
obj2.myMethod(20,12);
}}
Answer: It will throw a compilation error: More than one method with same name and
argument list cannot be given in a class even though their return type is different. Method
return type doesn’t matter in case of overloading.
Example:
Let us look at an example.
class Animal{
public void move(){
System.out.println("Animals can move");
}
}
class Animal{
public void move(){
System.out.println("Animals can move");
}
}
3. Overriding method can have different return type (if return types are not primitive) or user
defined return types( object types)