• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

overriding problem

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Instance variables is bound at compile time.
 
Our first order of business must be this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic