Thursday, 9 April 2015

Polymorphism in Java

Polymorphism is one of the four fundamental OOP concepts. The other three being -

Polymorphism Concept

Polymorphism, a Greek word, where poly means many and morph means change thus it refers to the ability of an object taking many forms.
The concept of polymorphism is often expressed as "One interface, multiple methods". Where one general class may have the generic method and the derived classes (classes which extend the general class) may add their own specific methods with implementation. At the time of execution "One interface" i.e. the general class will take "many forms" i.e. references of the derived classes (See example to have clarity). Note that Polymorphism is extensively used in implementing inheritance.

Polymorphism in Java

There are two types of polymorphism in java -

  • Compile time polymorphism (or Static polymorphism)
  • Runtime polymorphism (or Dynamic Polymorphism)

We can perform polymorphism in java by

Here we'll see an example of run time polymorphism to understand the concept of polymorphism.

// Super Class
class Shape{
 protected double length;
 Shape(double length){
  this.length = length;
 }
 void area(){
   
  }
}

// Child class
class Square extends Shape{
 //constructor to initialize length
 Square(double side){
  super(side); // calling the super class constructor  
 }
 //Overriding the area() method
 void area(){
  System.out.println("In area method of square");
  System.out.println("Area of square - " + length*length);
 }
}

// Child class
class Circle extends Shape{
 //constructor to initialize length
 Circle(double radius){
  super(radius); // calling the super class constructor 
 }
 //Overriding the area() method
 void area(){
  System.out.println("In area method of circle");
  System.out.println("Area of cirlce - " + 22/7*length*length);;
 }
}

public class PolymorphicTest {
 public static void main(String[] args){
  Shape shape;
  Square square = new Square(5.0); 
  Circle circle = new Circle(5.0); 
  // shape dynamically bound to the Square object referenced by square
  shape = square;
  // area method of the square called
  shape.area(); 
  // shape dynamically bound to the Circle object referenced by circle
  shape = circle; 
  // area method of the circle called
  shape.area();   
 }
}

Notice that in PolymorphicTest class' main method object shape of class Shape is declared and later that shape object takes the reference of square and then circle and calls the area method of the respective classes.

Output after running this program would be -

In area method of square
Area of square - 25.0
In area method of circle
Area of cirlce - 75.0

So it can be seen that the shape object holds the reference of the square object initially thus the area method of the Square class is called. Later the shape object holds the reference of the Circle object thus the area method of the Circle class is called.

Points to note -

  • Polymorphism, a Greek word, where poly means many and morph means change thus it refers to the ability of an object taking many forms.
  • Any number of classes can implement an interface and each class is free to provide their own implementation. That's how using interfaces, Java fully utilizes "one interface, multiple methods" aspect of polymorphism.
  • There are two types of polymorphism in Java, compile time polymorphism and run time polymorphism.
  • Method overloading in Java is an example of compile time polymorphism.
  • Method overriding in Java is an example of run time polymorphism.

That's all for this topic Polymorphism in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!


Related Topics

  1. Method overloading in Java
  2. Method overriding in Java
  3. interface in Java
  4. abstract class in Java
  5. Java OOP interview questions

You may also like -

>>>Go to Java Basics page

5 comments: