- Defining the multiple methods with same name associated with the same object is known as polymorphism.
- There are two types of polymorphism
- Static polymorphism (Compile time polymorphism ).
- Dynamic polymorphism (Run time polymorphism ).
Static polymorphism:
- The concept of defining multiple methods with same name and with different parameters within the same class is known as static polymorphism.
- we can achieve method overloading by using this static polymorphism.
- When Java encounters a call to an overloaded method, it simply executes the method whose parameters match the arguments used in the call.
- Overloaded methods may have different return types.
- No two functions/methods could be defined with the same name and with the same parameters of same data types , even by changing the return data type of the methods
- In java return data types are not considered with respect to static polymorphism.
Example program:
public class Sample{
public void methodName(){
System.out.println("no argument method");
}
public void methodName(int a, int b){
System.out.println("two argument method");
}
}
public static void main(String args[]){
Sample s= new Sample();
s.methodNmae();
s.methodNmae(2,4);
}
}
Output:
no argument method
two argument method
Why static polymorphism is known as compile time polymorphism?
- Out of multiple methods with same name which method is to get executed will be decided at the time of compilation itself.
Disadvantage of static polymorphism:
- There is every chance for polymorphism failing or breaking.
- As long as there is a method defined to accept the same data type value there is no problem
- If there are multiple alternative methods for whose parameters the value what we are passing as an argument matches with the same priority then JVM would not understand that it has to execute which method.
- lets see example programs for above mentioned problem.
package com.instanceofjavaforus;
public class poly {
void method(A a){
System.out.println("Method accepting object of class A a as an argument");
}
void method(B a){
System.out.println("Method accepting object of class B b as an argument");
}
public static void main(String args[]){
poly obj= new poly();
obj.method(null); // here compilation error will come
}
}
Output:
Gives a compilation error : "The method method(A a) is ambiguous for the type poly"
when we are passing a value which will be acceptable to all methods then jvm enters into tha state of ambiguity .
Same problem will occur when we are passing null value to println() method
Click here for explanation on System.out.println(null)
Dynamic polymorphism:
- The concept of defining multiple methods with same name and with same signature in super class and sub class known as Dynamic polymorphism.
- we can achieve method overriding by using this Dynamic polymorphism.
- Dynamic (run time) polymorphism is the polymorphism existed at run-time.
- Here, Java compiler does not understand which method is called at compilation time.
- Only JVM decides which method is called at run-time.
Program:
public class A{
public void methodA(){
System.out.println("Hello India"); //Base class
}
}
Public class B extends A{
public void methodA(){
System.out.println("Hello Indira"); //Derived Class
}
}
public class C{
public static void main(String args[]){
A a=new A();
A a1=new B(();
a.methodA();
a1.methodA();
}
}
Output:
Hello India
Hello Indira.
Why dynamic polymorphism is known as run time polymorphism?
Out of multiple methods with same name which method is to get executed will be decided at the time of runtime itself.
very good post..congrats bro
ReplyDeletevery good keep it...
ReplyDelete