When you call printname() on an object of the Test class, it's calling the printname() method in Exam (Test inherits it from there).
But the line System.out.println(name) in Exam can only refer to the name variable in Exam, because the Exam class knows nothing about the Test class.
The important thing to remember is that overriding applies to methods, but not instance variables. Which name variable is used is decided at compile time.
posted 4 years ago
You are calling method, not instance variable of the super class. when you are calling the method of the super class it will get executed as it is. As per the method you have defined in super class it is printing the variable, but not you are calling the variable directly.
Onion rings are vegetable donuts. Taste this tiny ad:
the new thread boost feature brings a LOT of attention to your favorite threads