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

Differentiate between stringbuffer and stringbuilder in java.

552


In java, how many ways you can take input from the console?

522


What are the features of java?

533


Is arraylist an object in java?

602


How will you load a specific locale?

538






What is e in java?

541


State two differences between C and Java.

695


What are the core java topics?

565


Can I import same package/class twice?

496


Name few java.lang classes introduced with java 8 ?

537


Difference between a class and an object?

578


Can we use switch statement with strings?

622


hr interview how many minutes asking question

1568


Describe what happens when an object is created in java ?

549


Can Exception handling we can handle multiple catch blocks?

644