oop
4. Variable Hiding: References vs Objects
Analyze the code, write your predicted output, and then reveal the solution.
Scenario
What happens when both Parent and Child have a variable with the same name? Unlike methods, variables are NOT overridden.
Code Block
class Super { String name = "SuperClass"; } class Sub extends Super { String name = "SubClass"; } public class Main { public static void main(String[] args) { Super obj = new Sub(); System.out.println("Name: " + obj.name); Sub actualObj = (Sub) obj; System.out.println("Actual Name: " + actualObj.name); } }
Take Your Shot
Predicted Output
Type exactly what you think will be printed in the console.