• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

doubt in variable in inheritance

 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Super
{
int var=10;
void display()
{
System.out.println("Inside Super");
}
}

public class Inheritance extends Super
{
int var=19;
void display()
{
System.out.println("Inside Inheritance");
}
public static void main(String s[])
{
Super i=new Inheritance();
i.display();
System.out.println(i.var); //1
}
}

At the line 1 while i am printing variable "i " it is printing Super class "i" . can you please clarify my doubts.

Thanks
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
because variables are statically bound (as against methods, which are dynamically bound.)
 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is only one rule to remember for situations like this:

Nothing else other than overriden methods depend on object type. Everything else other than overriden methods depend on the type of the reference variable

Hope this clears your doubt....

pls let me know and if I am wrong
 
Sujittt Tripathyrr
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Suhas

I cleared my doubt.
 
reply
    Bookmark Topic Watch Topic
  • New Topic