What is dynamic dispatch in java?

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


Please Help Members By Posting Answers For Below Questions

What is a java applet? What is an interface?

874


What is nan inf?

718


What does the string method compareto () do?

798


define polymorphism in java

871


What is a method type?

751


What is the difference between == and === javascript?

848


What is the purpose of encapsulation?

764


Similarity and difference between static block and static method ?

743


What does a za z0 9 mean?

780


Which methods cannot be overridden in java?

782


What is the difference between a constructor and a method?

894


What is a null class?

789


What mechanism does java use for memory management?

755


What about interthread communication and how it takes place in java?

821


What are the parts of a method?

774