wood burning stoves 2.0*
The moose likes Java in General and the fly likes Inheritance problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Inheritance problem" Watch "Inheritance problem" New topic
Author

Inheritance problem

Nitin Nigam
Ranch Hand

Joined: Jul 03, 2006
Posts: 129
Hello frns!!!
Here is my problem. I have a very simple superclass with a private int count and public getter and setter for the int.

Now i extend this superclass. My subclass will not inherit the int count, but it will inherit the methods setCount and getCount.
So what will happen if in my subclass, i try calling methods setCount and getCount. Answer is, "nothing Exceptional happens".

Here is my SuperClass


And here comes the baby SubClass




My question is, if count is not available with subClass, then what were setCount and getCount methods were setting and getting.


Nothing is impossible; for those who doesnt have to do it themselves.
myjotting.blogspot.com
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

count is not accessible in the subclass, because it is private.

However, the methods getCount and setCount are accessible in the subclass, because they are public. They're also inherited from the superclass.

If you call these methods on an instance of the subclass (and you haven't overridden these methods in the subclass) the code in the superclass is called. The code in the superclass can ofcourse access the variable count that's in the superclass.

When a method is inherited, it does not mean that a copy of the method is automatically made in the subclass - it sounds like you're thinking that inheritance works something like that.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Nitin Nigam
Ranch Hand

Joined: Jul 03, 2006
Posts: 129
Exactly!!!
I thought copy of public methods are made and put into subclasses. But it seems that when i call a certain inherited (non-overridden) method from a subclass, then the method of superclass is called and it operates on the variables of superclass only.
I think a major confusion is resolved.
Thanks.

But some confusion is still lying. What you are saying means the getCount method of superclass is being called and it gives me the value of int count that lies in superclass.
In the getCount method of superclass, i did

And when i call the getCount from subclass, system.out.println on first line tells me, that it has the object of subClass, and the second line prints the value of this.count (so it should be ,count of subclass, which doesnt actually exist)
which is actually the value of count in superclass.
[ December 04, 2006: Message edited by: Nitin Nigam ]
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

One very important thing to remember about inheritance is that it indicates an "is a" relationship.

An instance of a subclass is an instance of the superclass (with something extra on top). So in your case, an instance of SubClass is an instance of SuperClass.

So count is still there inside your instance of SubClass, but because it is private in SuperClass, it is only accessible to the methods in the SuperClass part of the object.
Sanjit Kumar
Ranch Hand

Joined: Dec 04, 2006
Posts: 35
the basic principle is that we cant access private variable outside the class or scope .
there will be no problem in overriding those methods but you cant access the count variable in getCount() as count is declared private.
 
 
subject: Inheritance problem
 
Similar Threads
Doubt About Access Modifier (Private)
getProperty not accessing correct object
Thread coordination.
problem with static counter
public vs private instance variables