filmov
tv
'Mastering Dynamic Method Dispatch in Java: A Deep Dive Tutorial | Java Polymorphism Explained'

Показать описание
Dynamic Method Dispatch
Method overriding forms the basis for one of Java’s most powerful concepts: dynamic method dispatch.
Dynamic method dispatch is the mechanism by which a call to an overridden method is resolved at run time, rather than compile time. Dynamic method dispatch is important because this is how Java implements run-time polymorphism.
Let’s begin by restating an important principle: a superclass reference variable can refer to a subclass object. Java uses this fact to resolve calls to overridden methods at run time. When an overridden method is called through a superclass reference, Java determines which version of that method to execute based upon the type of the object being referred to at the time the call occurs.
If a superclass contains a method that is overridden by a subclass, then when different types of objects are referred to through a superclass reference variable, different versions of the method are executed.
Example:
class A{
void callme() {
}
}
class B extends A{
void callme() {
}
}
class C extends A{
void callme() {
}
}
public class Dispatch {
public static void main(String[] args) {
A a=new A();
B b=new B();
C c=new C();
A r;
r=a;
r=b;
r=c;
}
}
The output from the program is shown here:
Inside A's call me method
Inside B's callme method
inside C's callme method
This program creates one superclass called A and two subclasses of it, called B and C. Subclasses B and C override callme() declared in A. Inside the main() method, objects of type A, B and C are declared. Also, a reference of type A, called r, is declared. The program in turn assigns a reference to each type of object to r and uses that reference to invoke callme().
As the output shows, the version of callme() executed is determined by the type of the reference variable, r, you would see three class to A’s callme() method.
#programmer
#Coding
#DeveloperLife
#TechTalk
#CodeSnippet
#SoftwareDevelopment
#WebDevelopment
#CodeNewbie
#ProgrammingTips
#LearnToCode
Method overriding forms the basis for one of Java’s most powerful concepts: dynamic method dispatch.
Dynamic method dispatch is the mechanism by which a call to an overridden method is resolved at run time, rather than compile time. Dynamic method dispatch is important because this is how Java implements run-time polymorphism.
Let’s begin by restating an important principle: a superclass reference variable can refer to a subclass object. Java uses this fact to resolve calls to overridden methods at run time. When an overridden method is called through a superclass reference, Java determines which version of that method to execute based upon the type of the object being referred to at the time the call occurs.
If a superclass contains a method that is overridden by a subclass, then when different types of objects are referred to through a superclass reference variable, different versions of the method are executed.
Example:
class A{
void callme() {
}
}
class B extends A{
void callme() {
}
}
class C extends A{
void callme() {
}
}
public class Dispatch {
public static void main(String[] args) {
A a=new A();
B b=new B();
C c=new C();
A r;
r=a;
r=b;
r=c;
}
}
The output from the program is shown here:
Inside A's call me method
Inside B's callme method
inside C's callme method
This program creates one superclass called A and two subclasses of it, called B and C. Subclasses B and C override callme() declared in A. Inside the main() method, objects of type A, B and C are declared. Also, a reference of type A, called r, is declared. The program in turn assigns a reference to each type of object to r and uses that reference to invoke callme().
As the output shows, the version of callme() executed is determined by the type of the reference variable, r, you would see three class to A’s callme() method.
#programmer
#Coding
#DeveloperLife
#TechTalk
#CodeSnippet
#SoftwareDevelopment
#WebDevelopment
#CodeNewbie
#ProgrammingTips
#LearnToCode