Polymorphism In Java - OOPS concept explained

Polymorphism is one of the fundamental Object Oriented Programming concepts and it can be achieved by differentiating the same named methods(two or more) either by its input parameter type or by the number of parameters.

Why need polymorphism?

To understand polymorphism in a better way let's take a real-time example, Consider you have an empty 1litre bottle and if that bottle contains water it's a water bottle and if it contains ketchup then it's a ketchup bottle so here the same 1-liter bottle showing different behavior based on the input(water/ketchup) type.

Similarly, in the programming term, a bottle() can be in two or more different forms based on input type(fillings in the bottle) or the number of inputs(water or water with ice).



Two types of polymorphism

  • Compile-time (overloading) 
  • Run-time (overriding)


    
Compile-time polymorphism: Within a class or interface when two or more similar named methods differ by the number of inputs or type of input and the Java compiler decides which method to load based on the parameters passing during the method call is known as method overloading. It's also known as static polymorphism. 

Java Method Overloading Example

public class Calculator{

public void add(int a, int b) {}

//Difference in data type

public void add(float a, float b) {}

//Difference in number of input

public void add(int a, int b, int c) {}

}


In this Java overloading example the add() methods are overloaded by the type or the number of inputs. When the programmer calls the add() method then based on the input passing through the add() method, the respective method will be accessed. If you pass three integer parameters in the add() method then add(int a, int b, int c) will be called.

String method subString() is a real-time or inbuilt overloading example from Java libraries. The method subString() can accept one parameter or two parameters.

subString(2);

subString(2,5);

Run-time polymorphism: Method overriding can be achieved by inheritance methodology. When the parent class and child class have a similar name method(same number/type of input parameters) then at run-time, the child class method will override the parent class method. It's also known as dynamic polymorphism.

Java Method Overriding Example

public class Calculator{

public void print() {

System.out.println("Hello World");

}

}

class Maths extends Calculator{

public void print() {

System.out.println("Hello");

}

public static void main(String args[]) {

}

}


In this Java overriding example print() method of the parent class will be overridden by the child class print() method and print "Hello World" in the console during run-time. 

The object equals() method is an inbuilt overriding example from Java libraries. The String parent class is an Object class child and it overrides the equal() method, the string equals() checks whether two strings are equal or not and the object equals() method indicates whether some other object is "equal to" this one

Post a Comment

Previous Post Next Post
B