oop
23. Dynamic vs Static Binding: The Deep Dive
Analyze the code, write your predicted output, and then reveal the solution.
The Scenario
Polymorphism relies on binding. Some things are bound at compile time (Static), others at runtime (Dynamic). Understanding which is which is crucial for predicting execution flow.
class Super { static void staticMethod() { System.out.println("Super Static"); } void instanceMethod() { System.out.println("Super Instance"); } String type = "Super Var"; } class Sub extends Super { static void staticMethod() { System.out.println("Sub Static"); } void instanceMethod() { System.out.println("Sub Instance"); } String type = "Sub Var"; } public class Main { public static void main(String[] args) { Super obj = new Sub(); obj.staticMethod(); obj.instanceMethod(); System.out.println(obj.type); } }
Take Your Shot
Predicted Output
Type exactly what you think will be printed in the console.