Polymorphism
Polymorphism is an OOP concept which we can perform a single action in a multiple ways. Polymorphism is derived from two Greek words:
'Poly' means many
'Morphs' means forms.
So polymorphism means many forms. Polymorphism allows you to define one interface and has multiple implementations. I know it sounds confusing. Don’t worry we’ll be discussing it in details.
The best real life example is of an Air Conditioner. It’s a single device that performs different actions according to different circumstances. During summer times it cools the air and during winter times it heats the air.
Another best example is humans. We have different behaviors in different situations in front of different people.
What is the need of polymorphism?
Imagine that you are the director of the company and you are going to conduct a meeting for the employees in your office. Instead of calling the employees of different sectors like employees of HR Department, employees of QA Department and employees of developing department you simply called as “All employees should attend the meeting”. This reduced the complexity of calling each and every employees separately.
Now from programming point of view, if you have ever used java just for basics them you must know about the method “print()”. It is an example of polymorphism.
public class Helloworld {
public static void main(String[] args) {
System.out.print("Helloworld"); //String
System.out.print(10); //int
System.out.print(10.5); //double
}
}
You can see that we have to remember only one function name to print all the different values instead of having different functions to print the values of different data type. It reduces our complexity.
It’s cool right!!!
Types of Polymorphism
1. Method Overloading
1. Method Overloading
2. Method Overriding
1. Method Overloading (Compile time polymorphism/ Early Binding)
Method overloading is a feature that allows a class to have two or more methods that having same method name, if their argument lists are different.
Argument list could differ in
1. Number of parameters
2. Data type of parameters
3. Sequence of data type of parameters
Let’s try an example
public class Test {
public void addNumbers(int a) {
System.out.println("Number is" + " " + a);
}
// No of parameters
public void addNumbers(int a, int b) {
int total = a + b;
System.out.println("Total is" + " " + total);
}
// Data type of parameters
public void addNumbers(int a, double b) {
double total = a + b;
System.out.println("Total is" + " " + total);
}
// sequence of data type
public void addNumbers(double a, int b) {
double total = a + b;
System.out.println("Total is" + " " + total);
}
public static void main(String[] args) {
Test test = new Test();
test.addNumbers(10);
test.addNumbers(10, 5);
test.addNumbers(10, 2.0);
test.addNumbers(2.3, 3);
}
}
Do you have any idea of the output?
Here it is
Here it is
And another interesting fact is Overload method may have different return types.
Let’s try an example for such scenario :
public class Test
{
public void add (int a)
{
System.out.println ("a: " + a);
}
double add(double a) {
System.out.println("double a: " + a);
return a+a;
}
}
Test.java
public class MethodOverloading
{
public static void main (String args [])
{
Test Obj = new Test();
double result;
Obj .add(20);
result = Obj .add(10.5);
System.out.println("Result is : " + result);
}
}
MethodOverloading.java
Test. java and MethodOverloading. java are in the same package. Test.java has two methods with the same method name and the different data types of parameter. Have you noticed that the return type of the above methods are different? yeah, one is void other one returns double value. To access those method in the main class (Method Overloading.java) we have to create Test object. Through the test object we'll be able to access those methods.
Here it is
It's cool right !!!
That's all about Method Overloading :)
Method Overriding (Run time polymorphism/ Late Binding)
Declaring a method in subclass which is already present in it’s parent or base class is known as method overriding.
Let's look at an example :
Parent class :
public class SuperClass {
public void print(){
System.out.println("Parent Class");
}
}
Child Class
public class SubClass extends SuperClass {
//Method overriding
public void print(){
//This will call the print() of parent class
super.print();
System.out.println("Subclass");
}
public static void main(String[] args) {
SubClass sub=new SubClass();
sub.print();
}
}
Output is :
Note this point here ‘Super’ Key word used inside the child class/sub class to call the method that is defined in the parent class/super class.
Ok upto now we saw a brief explanation about 'Method Overloading' and 'Method Overriding'.
So you might ask what are the differences between Method Overloading and Method Overriding
Here it is
Method Overloading | Method overriding |
Happens at compilation time | Happens at run time |
Static method can be overloaded | Static cannot be overridden |
Overloading is being done in the same class or between two classes | Overriding is being done between parent and child class |
Private ,final methods can be overloaded | Private,final methods cannot be overriden |
It's time for quiz
Let's play
Select valid/ invalid cases of method overloading
1. . int myMethod (int a, int b, float c)
int myMethod (int num1, int num2, float num3)
2.int myMethod (int a, int b)
int myMethod (float num1, float num2)
Answers :
1.Compilation error because argument lists are exactly same. Both of them are having same number, data type and the same sequence of data type arguments.
2.Valid case for overloading because here data types of arguments are different
It's very useful to get the basic knowledge of polymorphism. & I learned difference between method overriding & over loading .. Good job. Well done����
ReplyDeleteClear explanation Great work 👍
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteIt is giving a clear idea about polymorphism, making it easier to learn .Well done 🖒🖒👌👌
ReplyDeleteIt is easy to understand.... Good work :)
ReplyDeleteGood Job
ReplyDelete