This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes overriding problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "overriding problem" Watch "overriding problem" New topic
Author

overriding problem

Sreenivasarao Venepalli
Greenhorn

Joined: Aug 23, 2006
Posts: 1
Please see the below code
class Exam{
protected String diff = " easy ";
public void printDiff(){
System.out.println(this.diff);
}
}
public class Test1 extends Exam{
private String diff = " killing ";
public static void main(String[] args){
Test1 test = new Test1();
test.printDiff();
System.out.println(test.diff);
}
}
it is giving output as "easy" and "killing" I am invoking the super class method with subclass object, here object type is subclass only but at the time of execution why it is considering super class variable? and I tried this way also like I override parent class method in subclass and I tried to execute and it is printing "killing" 2 times..why it is?can anybody explain exactly what is happening here?

Thanks in advance.
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
Instance variables is bound at compile time.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: overriding problem