File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Inheritance --- Assigning Sub class Object to Super Class Reference Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Inheritance --- Assigning Sub class Object to Super Class Reference" Watch "Inheritance --- Assigning Sub class Object to Super Class Reference" New topic
Author

Inheritance --- Assigning Sub class Object to Super Class Reference

Raghavendra Nittur
Greenhorn

Joined: Feb 19, 2007
Posts: 29
Hi All,

I have following piece of code.


Am getting Output as,
Private member of A x=10
In super class A
In Subclass B

My doubt is, since we are creating object of subclass B, private member of suerclass A will not be inheritated. but still here am able to get the value of x. How this could be possible....? Please anyone let me know the reason behind this.

Regards, Raghav.

[ Edited to use code tags - Paul Sturrock ]
[ February 23, 2007: Message edited by: Paul Sturrock ]
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Not an advanced question. Moving...


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Abhijith Prabhakar
Ranch Hand

Joined: Dec 29, 2006
Posts: 56
Class B is not inheriting x, but compiler put a call to super class constructor.

class B{
B(){
super(); //Auto generated
Sys..
}
}
So, while executing Class A constructor, x value will be outputted.
Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
What you are seeing is due to the fact that when a sub classes is created, all of its parents constructors will be called.

Sort of like a reverse waterfall.

So when you say, create me a new version of B, the first thing that B's construct does is call its parents construct with a call to super.

So you are not really access A.x from B.

Not to confuse things further, the whole constructor chaining get more complicated with: default constructor, automatically generated constructor's etc. I am still trying to get my head around it.

G
Raghavendra Nittur
Greenhorn

Joined: Feb 19, 2007
Posts: 29
Originally posted by Abhijith Prabhakar:
Class B is not inheriting x, but compiler put a call to super class constructor.

class B{
B(){
super(); //Auto generated
Sys..
}
}
So, while executing Class A constructor, x value will be outputted.

Hi Abhijith, i agree with you. But x is a member of class A, and no where we are creating object of A. Object of B will not have memory allocated for x. hope am able to convey my doubt to you now.
Raghavendra Nittur
Greenhorn

Joined: Feb 19, 2007
Posts: 29
Hi Gavin, ;-)

When i do,
A obj = new B();

Object of B is getting created, Only the reference is from super class A.
So the private member x in class A should not be allocated memory. ( since no where the object of A is getting created )
Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
I think, and I could be wrong, but when we say A.x is private and that B does not inherit A.x, what we are really saying is B does not inherit access to A.x

Consider a super class that encapsulates some data about a WheeledVehical, it has a private field _numWheels with public getters and setters.



Now consider a class Car that extends WheeledVehical, Car inherits the public getters and setters, but not the private field that getters and setters work on. Logically the private field must exist within Car, Car just has no knowledge of it, however it must be there for the inherited getters and setters to be able to work without throw an exception.

At least that is how I look at it.

G
[ February 23, 2007: Message edited by: Gavin Tranter ]
Abhijith Prabhakar
Ranch Hand

Joined: Dec 29, 2006
Posts: 56
Gavin is absolutely right and I think there is no question of memory allocation here. B does not have the knowledge that x exists, that's all.

But the output of "X" value you are getting is from class A() constructor. You have called this constructor from B doesn't mean that you cannot access it. Class A() method's have access to X at any time.
Raghavendra Nittur
Greenhorn

Joined: Feb 19, 2007
Posts: 29
Hi Gavin / Abhijith,

I agree with you guys that the output of "X" value am getting is from class A() constructor. Class A() method's have access to X at any time.

But what is the whole concept behind objects.....? Using new whenver object is created some amount of Heap memory will be allocated for
1. Members defined directly in the class.
and 2. Members inherited frm the inheritance chain. ( i mean from super classes).

my statement,
A obj = new B();

clearly says that heap is allocated only for members of B. Clearly x present should not be accessed since it is not at all allocated memory!!!


we can access members defined in class only when the object of that class is created or the members are static. am right in saying this?


Regards, Raghav
Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
Raghav,
A class is a defination of an object, its contract. When another class extends an object it inherits that defiination.

Therefore when you create a instance of B you are also implicitly creating an instance of A.

One of the abilities of OO lanaguages is define access levels to parts of a class to hide/protect behavior and state, private defines the most restrict access level.

So, B extends A, this means that B has a copy of everything that is in A, however, B is unaware of anything that has been marked as having private access.

In your example what happens is the compilor knows that in order to create B it must also create A. In your example X is accessed as a side effect of the compilor calling B's super constructor (A's constructor).
This has nothing to do with your attempt to assign B to its super type, the same result we be displayed if you did:


Evertime a subtype is created all of its super types are also created. In your example you will be able to call:

Yet you did not define these (public) methods. You will find that these methods form part of the definition for the Object class, the super type of all Classes.
These methods are NOT static, so in order for the methods to function they most have been created when B was created, in fact they were created when A made an implicit call to Object's constructor.

Gavin
Raghavendra Nittur
Greenhorn

Joined: Feb 19, 2007
Posts: 29
Gavin,

Thank you So much...
I got my doubt cleared.

So i conclude that, " Evertime a subtype is created all of its super types are also created". So Object of A also created and hence we have x available...

Thank you once again Gavin and Abhijith.

Please let me know where i can get my core Java Concepts Strongest...


Regards, Raghav
Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
Your welcome, glad I could be of hlep.

The Java tutorial site should be able to help you out:
Java Tutorial

Also I really like the head first series of books, I am using the head first design patterns book currently. It has a very informal style that I find easy to read.

Also, and it might sound strange, but once you have the basic rules of java down, try one of the Certifiaction Primer books, I have been coding Java since the late 90's and I am learning new things about java as I work my way though the book.
I am using:
SCJP 5 Book
Again it has a very informal style

hope this helps.
Gavin
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3646
    
  15
Originally posted by Raghavendra Nittur:
So Object of A also created


No. Only one object is created and that is of type B. However this object will contain all the fields defined in A. So, obj.x exists, you just can't access it directly. Change your main method to
and compile it.
Note that the error is 'x has private access in A' and not 'cannot find symbol', which shows that the field exists, it is just not directly accessible.


Joanne
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11406
    
  16

the variable x has to exist, or else you wouldn't be able to run the code in the A() constructor.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Raghavendra Nittur
Greenhorn

Joined: Feb 19, 2007
Posts: 29
Originally posted by Joanne Neal:


No. Only one object is created and that is of type B. However this object will contain all the fields defined in A.



HI, x is a private member of class A( i mean super class).
So how come object of B ( i.e, object of sub class) will have x?

Regards, Raghav
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Because B truly is an A. It is everything A is plus more. You can't take anything away when you write B. Let's add a method to your A ...

And now in some other class we create a B and pass it around ...

The private x still has to be there, no?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Raghavendra Nittur
Greenhorn

Joined: Feb 19, 2007
Posts: 29
Originally posted by Stan James:
Because B truly is an A. It is everything A is plus more. You can't take anything away when you write B.
The private x still has to be there, no?


James you mean, an object of subclass will have all the mombers defined in its super class heirarchy( Including private members)....
But only thing is i cann't access from member functions of B directly. thts what you mean Right?

Regards, Raghav
Raj Kumar Bindal
Ranch Hand

Joined: Apr 15, 2006
Posts: 418
java does not have any size of operator like c++.because in c++ by seeing the size of derived class ,we can get that private members are also inherited in the derived class but we are not able to access them directly.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inheritance --- Assigning Sub class Object to Super Class Reference