This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt in variable in inheritance

 
Sujittt Tripathyrr
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • 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
 
Neelesh Bodas
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
because variables are statically bound (as against methods, which are dynamically bound.)
 
Suhas Wadadekar
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks Suhas

I cleared my doubt.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic