Types of polymorphism in java-Runtime and compile time polymorphism.
In the last instructional exercise we talked about Polymorphism in Java. In this guide we will see sorts of polymorphism. There are two kinds of polymorphism in java:
1) Static Polymorphism otherwise called gather time polymorphism
2) Dynamic Polymorphism otherwise called runtime polymorphism
Compile time Polymorphism (or Static polymorphism)
Polymorphism that is settled during compiler time is known as static polymorphism. Technique over-burdening is an illustration of aggregate time polymorphism.
Technique Overloading: This permits us to have more than one strategy having a similar name, if the boundaries of strategies are distinctive in number, arrangement and information sorts of boundaries. We have effectively talked about Method over-burdening here: If you didn't peruse that direct, allude: Method Overloading in Java
Illustration of static Polymorphism
Strategy over-burdening is one of the manner in which java upholds static polymorphism. Here we have two meanings of a similar strategy add() which add technique would be called is dictated by the boundary list at the aggregate time. That is the explanation this is otherwise called order time polymorphism.
class SimpleCalculator
{
int add(int a, int b)
{
return a+b;
}
int add(int a, int b, int c)
{
return a+b+c;
}
}
public class Demo
{
public static void main(String args[])
{
SimpleCalculator obj = new SimpleCalculator();
System.out.println(obj.add(10, 20));
System.out.println(obj.add(10, 20, 30));
}
}
Ouput:
30
60
Runtime Polymorphism (or Dynamic polymorphism)
It is otherwise called Dynamic Method Dispatch. Dynamic polymorphism is an interaction where a call to an abrogated technique is settled at runtime, that is the reason it is called runtime polymorphism. I have effectively talked about strategy abrogating in detail in a different instructional exercise, allude it: Method Overriding in Java.
Model :
In this model we have two classes ABC and XYZ. ABC is a parent class and XYZ is a kid class. The kid class is superseding the strategy myMethod() of parent class. In this model we have kid class object doled out to the parent class reference so to figure out which strategy would be called, the sort of the article would be resolved at run-time. It is the kind of article that figures out which form of the technique would be called (not the sort of reference).
To cmprehend the idea of abrogating, you ought to have the fundamental information on legacy in Java.
class ABC{
public void myMethod(){
System.out.println("Overridden Method");
}
}
public class XYZ expands ABC{
public void myMethod(){
System.out.println("Overriding Method");
}
public static void main(String args[]){
ABC obj = new XYZ();
obj.myMethod();
}
}
Out:
Superseding Method
At the point when a superseded technique is called through a reference of parent class, at that point sort of the item figures out which strategy is to be executed. Along these lines, this assurance is made at run time.
Since both the classes, kid class and parent class have a similar strategy animalSound. Which form of the method(child class or parent class) will be called is resolved at runtime by JVM.
Barely any additional superseding models:
ABC obj = new ABC();
obj.myMethod();
/This would call the myMethod() of parent class ABC
XYZ obj = new XYZ();
obj.myMethod();
/This would call the myMethod() of youngster class XYZ
ABC obj = new XYZ();
obj.myMethod();
/This would call the myMethod() of youngster class XYZ
In the third case the strategy for youngster class is to be executed in light of the fact that which technique is to be executed is controlled by the kind of item and since the article has a place with the kid class, the kid class rendition of myMethod() is called.
0 Comments:
Post a Comment