aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark ""extends" keyword and instance variables. " Watch ""extends" keyword and instance variables. " New topic
Author

"extends" keyword and instance variables.

Nikhil Pujari
Greenhorn

Joined: Oct 31, 2011
Posts: 21

This question is about overriding and instance variables. Now I know instance variables cannot be overridden, but I can't get this. I found this in the Rules Roundup game.



I thought the System.out.println(name) is replaced within the Test class as System.out.println(this.name). But that doesn't seem to be the case. Why is this?


Einstein: A problem cannot be solved in the same state of mind in which it was created.
More motivation: http://www.youtube.com/watch?v=_7xfyDjCFqs
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575



just paste this in your subclass and then test.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4240
    
    7

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.
raju salla
Greenhorn

Joined: Jan 05, 2012
Posts: 18
Hello,
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.

thanks.
 
Consider Paul's rocket mass heater.
 
subject: "extends" keyword and instance variables.
 
Similar Threads
JavaRanch #19
difference between redefining and overridding
Vararg Problem
Can you explain this with code...
var-args question from skill test