Answer Posted / mahipal
Dynamic dispatch is a mechanism by which a call to an
overridden method is resolved at run time, rather than
compile time.
Dynamic dispatch is also known as run-time polymorphism in java.
Here is an example
/*Here is an example of run-time polymorphism*/
/* Run-time Polymorphism is acheived by method overriding*/
/*here we create a super class A having one method fun1*/
/*here we extends Class by Class B*/
class A
{
public void fun1(int x)
{
System.out.println("X in Class A is : "+ x);
}
}
class B extends A
{
public void fun1(int x)
{
System.out.println("X in Class B is : "+ x);
}
}
public class Main
{
public static void main(String[] args)
{
A obj; // we declare variable obj as A type
obj= new A(); // allocate to variable obj
obj.fun1(2); // line 2 (prints "x in Class A is : 2")
obj = new B();
obj.fun1(10); // line 4 (prints ""int in Class B is : 5")
}
}
| Is This Answer Correct ? | 2 Yes | 0 No |
Post New Answer View All Answers
Is java a digit method?
What is static class
How many threads can I run java?
What is implicit object in java?
what is the purpose of the wait(), notify(), and notifyall() methods? : Java thread
If a method is declared as protected, where may the method be accessed?
How do you know if a value is nan?
Is singleton thread safe in java?
Why java uses the concept of the string literal?
Give the hierarchy of inputstream and outputstream classes.
Can we sort list in java?
What is join () in java?
Is 64bit faster than 32 bit?
What is a jagged array in java?
Why is string class considered immutable?