This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

overriding problem

 
Sreenivasarao Venepalli
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instance variables is bound at compile time.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic