What is dynamic dispatch in java?

Answers were Sorted based on User's Feedback



What is dynamic dispatch in java?..

Answer / jamia hamdard

Dynamic method dispatch is polymorphism!

we create object dynamically like:

animal ani = new animal();

If there are two different classes 'dog' and 'horse' that inherit from the class 'animal', then we can assign the 'object' of these two classes to the reference variable 'ani' made above. like:-

animal ani = new dog();//since dog inherits from animal.
or
animal ani2 = new horse();//since horse inherits from animal.

polymorphism is same name and having different signatures like 'dog' and 'horse' both are animals.
Its use is to make polymorphic arrays which stores Objects of different classes. The type of the array is that of the 'super class' which is 'animal' here.

thanks!!

Is This Answer Correct ?    9 Yes 0 No

What is dynamic dispatch in java?..

Answer / omprakashsingh7

dynamic dispatch is the process of assigning a sub class
object to super class reference variable.

Is This Answer Correct ?    7 Yes 1 No

What is dynamic dispatch in java?..

Answer / manishsoni

class A
{
void callme()
{
System.out.println("Inside A's callme method");
}
}
class B extends A
{
void callme()
{
System.out.println("Inside B's callme method");
}
}
class C extends A
{
void callme()
{
System.out.println("Inside C's callme method");
}
}
class Dispatch
{
public static void main(String args[])
{
A a = new A(); // object of type A
B b = new B(); // object of type B
C c = new C(); // object of type C
A r; // obtain a reference of type A

r = a; // r refers to an A object
r.callme(); // calls A's version of callme

r = b; // r refers to a B object
r.callme(); // calls B's version of callme

r = c; // r refers to a C object
r.callme(); // calls C's version of callme
}
}
MoNu

Is This Answer Correct ?    7 Yes 2 No

What is dynamic dispatch in java?..

Answer / 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

More Core Java Interview Questions

Can a static method be final?

0 Answers  


what is a green thread? : Java thread

0 Answers  


I have 100 records in a table with two rows. I need to display 10 records per page like Google Search. I need only the Logic(Pagination) in Pure Java. No JSP and all..Thanks in Advance...

2 Answers   Cybernet,


What best practices should you follow while writing multithreaded code in java?

0 Answers  


Is singleton class thread safe?

0 Answers  






Can we sort set in java?

0 Answers  


How can you say java is object oriented?

0 Answers  


Difference between final and effectively final ? Why is effectively final even required ?

0 Answers  


What is a condition in programming?

0 Answers  


Define how can we find the actual size of an object on the heap?

0 Answers  


Explain OOPs concept.

0 Answers   Syntel, Visa,


Can a constructor have different name than a class name in java?

0 Answers  


Categories