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

Can we override static methods in java?

825


Why Java is not pure Object Oriented language?

875


What is lexicographically smallest string?

822


What is nan inf?

699


What is floor math?

697


State the difference between strings and arrays.

866


What are the parts of a method?

761


Does a function need a return?

748


What is a default method?

770


How many threads does a core java have?

787


How to use arraylist in java netbeans?

740


does java support pointers?

755


Which method must be implemented by all threads?

977


explain copyonwritearraylist and when do we use copyonwritearraylist?

744


What data structures are used to perform recursion?

811