• Post Reply Bookmark Topic Watch Topic
  • New Topic

Inheritance query  RSS feed

 
Chetan Raju
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

In the above code, subclass Sample is calling meth(). As per inheritance principles, the method meth() is inherited in the subclass and can be accessed directly as if it is declared in the subclass itself. The method is printing a instance variable i which is declared private in super class and hence not inherited in subclass. Still, the code prints 10 as answer. Can anyone please elaborate.
 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is calling meth(), which is a member of Super, so has access to all class level variables.


Think of inheritance as a building, each floor being a class in the hierarchy. The floor contains all the methods, variables, ect of its own class. A ladder with some guy on it is the reference. The entire building is the object. The first floor(ignoring the fact that Object is really the base class) is Super, the second is Sample. The ladder reaches to the top of the building since the reference is of Sample. When meth() is called the guy looks down(the inside is transparent), he grabs the first method he sees with that name. That method lives in the first floor, so it can access i.

I know that is a silly example, but it is what my professor used in my very first programming class. It really helped to see what is going on, especially with base class references to derived class object. Hopefully it makes sense, it is much easier to draw it out and visualize it.


Override meth() in Super and try to print out i and see what happens.
[ August 22, 2006: Message edited by: Rusty Shackleford ]
 
Chetan Raju
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rusty, for the simple explanation. However, as per some of the java books available, the explanation given is that "The methods of super class are inherited to subclass. That is the same as declaring a method in subclass". This seems a bit confusing statement. Any comments ?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you call meth() on an instance of Sample, you can imagine Java looks for the method and can't find it. So it looks up the inheritance tree, finds class Super, finds the method and calls it. Now we're in code defined by Super which can see "i" even though it is private.

What if you overrode meth() in Sample? Java would look for meth() on Sample and find it. But Sample can't see "i" so Java gives you compile errors if you even try.

The way I visualize Java hunting down methods is not exactly true, but it helps me keep things straight. Did it do anything for you?
 
Ashok Kumar Babu
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What James told is Correct. Override that method in Sample class as

public void meth() {
System.out.println(i);
}

Will tell you clearly that I dont know the variable I. Since it is not overridded now it is having access to all its class members.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!