Polymorphism Part 1 (Compile Time Polymorphism)
https://www.facebook.com/techaddaa/
Hello Friends,
This is second post in core java concepts blog. This series is specially designed for beginners to help them in their learning.
Introduction -
There are many concepts like Classes, Object, Polymorphism, Inheritance etc. Here we will explore polymorphism and learn types of polymorphism, it s'advantages along with implementation in java language.
Prerequisite -
- https://oopsconceptsjava.blogspot.com/2020/08/classobjects.html
- Zeal for learning.
What is Polymorphism?
- Compile time polymorphism
- Run time polymorphism
1. Compile time polymorphism is also known as static polymorphism because this behaviour can be determine or predict at the time of program compilation. This is achieved with the help of function overloading in java.
Function/Method overloading is flexibility where we can have same name for multiple functions/methods. Compiler determines function calling on the basis of no of arguments, type of arguments.
Here we have three overloaded methods where name is same "add" but ultimately which method will be invoked that will depend on method arguments.
public void add(int a, int b); - this method will add two numbers
public void add(int a, int b, int c); - this method will add three numbers
public void add(String a, String b); - - this method will add two strings
P.S. (***) -
Method return type does not impact overloading behaviour. Below two methods are not case of overloading and this will give compile time error.
public int add(int a, int b);
public void add(int a, int b);
To understand compile time polymorphism in details, lets implement and understand. if we know how to implement concept in program then you have better understanding than if you know only theory. We will create class explained in above example.
Define the class "Calc". Template/blueprint.
package com.utility;
public class Calc {
public void add(int a, int b) { // this method will add two numbers
System.out.println("a+b = " + a+b );
}
public void add(int a, int b, int c) { // this method will add three numbers
System.out.println("a+b+c = " + a+b+c );
}
public void add(String a, String b) { // this method will add two strings
System.out.println("a +b = " + a.concat(b) );
}
}
We have learned define class and object creation for that class in last post. So lets create object of Calc class. With that object we will call overloaded method add.
Create one more class for demo. - PolymorphismDemo.java
package com.demo;
import com.utility.Calc;
public class PolymorphismDemo {
public static void main(String[] args) {
Calc calc = new Calc();
calc.add(5,6);
calc.add(2,3,5);
calc.add("Compile Time"," Polymorphism");
}
}
Output -
a+b = 56
a+b+c = 235
a +b = Compile Time Polymorphism
Process finished with exit code 0
Source code download -
https://github.com/sarveshoza/PolymorphismInJava/tree/compiletimepolymorphism
Advantages-
Bonus-
In this section, we will see how return type does not play any role in method overriding.
Let's modify our Calc class and add one more method in that class.
public int add(int a, int b){
return (a+b);
}
We will see compilation error. It means if two methods have same signature but different return type then those method would not qualified as overloaded method.
Error:(16, 16) java: method add(int,int) is already defined in class com.utility.Calc
Till here, we have learned -
1. Create class
2. Create Object
3. Define methods
4. Overload methods
5. Method Calling
Comments
Post a Comment