OOPs Concepts in JAVA - Polymorphism

 What is Polymorphism

Polymorphism is defined as one task can be perform with different methods

Types of Polymorphism

  • Method Overloading
  • Method Overriding

Method Overloading

> When methods with similar name are created within a Class but with different number of parameters, different orders then it is call method overloading

Example of methods overloading

Class A{

Add (int a, int b);
Add(int a, String b);
}
Here Add methods has different number of parameter/different types of parameters hence this is method overloading 


Practical application of methods overloading 
  • Being an Automation Tester, we have been using methods overloading in our project while creating the common functions. 
  • Assertion is one of the best example of method overloading in which Assert Class has many methods in soft and hard asserts (more about Assertion in the next blog)

Method Overriding

> When methods with same number of parameter, type, exact signature is same is created outside the same class then its called method overriding.

Example of methods overriding

Class A{

Add (int a, int b);
}

Class B{

Add (int a, int b); 
}


Here Add methods has same signatures in both the classes hence the method has been override with method overriding polymorphism.

Practical application of methods overriding 
  • When we use method overriding, we can easily change the implementation of any method in different classes as per our need. 


Comments