This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt in variable in inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doubt in variable in inheritance" Watch "doubt in variable in inheritance" New topic
Author

doubt in variable in inheritance

Sujittt Tripathyrr
Ranch Hand

Joined: Jun 21, 2006
Posts: 96
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

Joined: Jul 20, 2006
Posts: 107
because variables are statically bound (as against methods, which are dynamically bound.)
Suhas Wadadekar
Ranch Hand

Joined: May 16, 2006
Posts: 95
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

Joined: Jun 21, 2006
Posts: 96
Thanks Suhas

I cleared my doubt.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: doubt in variable in inheritance
 
Similar Threads
Inheritance query
primitive value
Casting int to short behind the scenes.
Constructor Chaining
need help