Java Software Engineer Question:
Download Job Interview Questions and Answers PDF
Explain me what is runtime polymorphism or dynamic method dispatch?
Answer:
In Java, runtime polymorphism or dynamic method dispatch is a process in which a call to an overridden method is resolved at runtime rather than at compile-time. In this process, an overridden method is called through the reference variable of a superclass. Let’s take a look at the example below to understand it better.
class Car {
void run()
{
System.out.println(“car is running”);
}
}
class Audi extends Car {
void run()
{
System.out.prinltn(“Audi is running safely with 100km”);
}
public static void main(String args[])
{
Car b= new Audi(); //upcasting
b.run();
}
}
class Car {
void run()
{
System.out.println(“car is running”);
}
}
class Audi extends Car {
void run()
{
System.out.prinltn(“Audi is running safely with 100km”);
}
public static void main(String args[])
{
Car b= new Audi(); //upcasting
b.run();
}
}
Download Java Software Engineer Interview Questions And Answers
PDF
Previous Question | Next Question |
Explain me what is the difference between String s = "Test" and String s = new String("Test")? Which is better and why? | Tell me public static void main(String args[])? |