Let’s learn polymorphism in java.
Polymorphism in java
Polymorphism literally means many forms. Polymorphism is the ability of a method to perform different tasks. It can be achieved through method overloading and method overriding.
For example: Wood has many forms table, chair, dinning table etc.
Example : Polymorphism in java
class Shapes { public void sides() { System.out.println("A shape can have many sides."); } } class Square extends Shapes { public void sides() { System.out.println("square has four sides."); } } class Triangle extends Shapes { public void sides() { System.out.println("triangle has three sides."); } } class PolymorphismExample { public static void main(String[] args) { Shapes obj1 = new Shapes(); Shapes obj2 = new Square(); Shapes obj3 = new Triangle(); obj1.sides(); obj2.sides(); obj3.sides(); } }
Output:
A shape can have many sides.
square has four sides.
triangle has three sides.
In the above example we have created a parent class Shapes and two child classes Square and Triangle. All these classes have method sides().
sides() method show the sides of particular shape. In the above example process of showing sides of square is different from showing sides of triangle.
Therefore, sides() method behaves differently in different classes. Hence sides() method is polymorphic.
Types of polymorphism in java
There are two types of polymorphism
- Compile time polymorphism (static polymorphism)
- Runtime polymorphism (dynamic polymorphism)
Compile time polymorphism (static polymorphism):
Java compiler is able to handle method call at compile time hence known as compile time polymorphism or static polymorphism.
Compile time polymorphism is achieved through method overloading. The conditions Below are ways to overload a method,
- By changing number of arguments.
- By changing data types of arguments.
Let’s see example on compile time polymorphism by changing number of arguments.
class Demo { void show(int x, int y) { System.out.println("2"); } void show(int x, int y, int z) { System.out.println("3"); } public static void main(String[] args) { Demo obj = new Demo(); obj.show(20, 30); obj.show(20, 30, 40); } }
Output:
2
3
Now let’s example on compile time polymorphism by changing data types of arguments.
class Add { // method with two parameters static int addition(int a, int b) { return a + b; } // method with same name but three double parameters static double addition(double a, double b, double c) { return a + b + c; } } public class CompileTimeExample { public static void main(String[] args) { System.out.println(Add.addition(5, 3)); System.out.println(Add.addition(2.8, 5.2, 5.1)); } }
Output:
8
13.1
Runtime polymorphism (dynamic polymorphism)
In runtime polymorphism call to an overridden method is determined at runtime. Runtime polymorphism is implemented using method overriding.
Method overriding means when a method in a subclass has the same name but in different class with same arguments.
Here number of arguments can be same, type of arguments can be same, sequence of arguments and there should be concept of inheritance then it is said to override the method in super class. Let’s see example on runtime polymorphism.
class Car { void accelerate() { System.out.println("car accelerating"); } } class Baleno extends Car { void accelerate() { System.out.println("baleno accelerating"); } public static void main(String[] args) { Car obj1 = new Car(); obj1.accelerate(); Baleno obj2 = new Baleno(); obj2.accelerate(); } }
Output:
car accelerating
baleno accelerating
NOTE:
- Runtime polymorphism is implemented only through IS-A relationship, i.e, through inheritance.
- Runtime polymorphism is handled by JVM.