Junior — Middle
What is meant by the process of selecting and calling a method during program execution?
sobes.tech AI
Answer from AI
The process of selecting and invoking a method during program execution is called dynamic (or late) binding. This means that the specific implementation of a method is determined not at compile time, but at runtime, which allows for polymorphism.
In Java, this occurs, for example, when calling methods through references to a base class, where the actual method called depends on the type of the object to which the variable refers.
Example:
class Animal {
void sound() { System.out.println("Some sound"); }
}
class Dog extends Animal {
@Override
void sound() { System.out.println("Bark"); }
}
Animal a = new Dog();
a.sound(); // The Dog.sound() method will be called thanks to dynamic binding