Compile Time Polymorphism Vs Run Time Polymorphism
Compile Time Polymorphism
- Also known as Static Binding, Early Binding and Overloading.
- Compile Time Polymorphism is resolved during compiler time.
- Provide faster execution.
- Less flexible.
- Example of Compile Time Polymorphism is Method Overloading.
Program
class Calculator
{
int add(int a, int b)
{
return a+b;
}
int add(int a, int b, int c)
{
return a+b+c;
}
}
public class Result
{
public static void main(String args[])
{
Calculator obj = new SimpleCalculator();
System.out.println(obj.add(20, 30));
System.out.println(obj.add(20, 40, 10));
}
}
Output
50
70
Run Time Polymorphism
- Also known as Late Binding, Dynamic and Overriding.
- Run Time Polymorphism is an overridden method is resolved at runtime.
- Provide slow execution.
- More flexible.
- Example of Run Time Polymorphism is Method Overriding.
Program
class OverRidden
{
public void myMethod()
{
System.out.println("Overridden Method");
}
}
public class Result extends OverRidden
{
public void myMethod()
{
System.out.println("Overriding Method");
}
public static void main(String args[]){
OverRidden obj = new Result();
obj.myMethod();
}
}
Output
Overriding Method
![]() |
Compile Time Polymorphism VS Run Time Polymorphism |
0 Comments