In Java, the method that gets executed is determined by the actual type of the object at runtime, not the type of the reference variable. This is due to dynamic method dispatch, which allows overridden methods in subclasses to be called even when referenced as a superclass. For instance, if a `Product` reference points to a `Book` object, calling `display()` will run `Book`'s `display()` method if it's overridden.
Additional Notes
What determines which method body runs in Java?
Track: Java
Topic: Core Java
Focus: Upcasting and Dynamic Dispatch
Topics:Core JavaJavaUpcasting and Dynamic Dispatch