Forums Register Login

inheritance

+Pie Number of slices to send: Send


When a object of child class is created,the object of parent class will be created automatically? and object of parent class occupied the memory like it is created: a y=new a();

If the answer is negative, Then how variable of parent can be accessed in child's object?
+Pie Number of slices to send: Send
There is an attribute of the SUPERclass that allows the subclass to reference superclass variables(if they are public) and pass data to the superclasses constructor.

Have a look here for more information about the ins and outs of inheritance:
http://java.sun.com/docs/books/tutorial/java/IandI/super.html
+Pie Number of slices to send: Send
abalfazl : Your question is not very clear to me. Can you say more?

Jim ... ...
+Pie Number of slices to send: Send
 

abalfazl hossein wrote:

When a object of child class is created,the object of parent class will be created automatically?


True

abalfazl hossein wrote:and object of parent class occupied the memory like it is created: a y=new a();


False. Instead you can look at it this way: The parent is created first then the child is created on top of the parent and the child occupies the memory.

abalfazl hossein wrote:If the answer is negative, Then how variable of parent can be accessed in child's object?


As mentioned by Hunter above parent class constructor can be accessed by the keyword super() and parent class members can be accessed by the keyword super.memberName

Cheers,
Harshada
http://hrshdnik.blogspot.com/
+Pie Number of slices to send: Send
 

False. Instead you can look at it this way: The parent is created first then the child is created on top of the parent and the child occupies the memory.



Then at same time that child occupied the memory,The object of parent is created and occupied the memory.with all its members: public and private and methods... Right?
+Pie Number of slices to send: Send
They're not called "parent" and "child" but "superclass" and "subclass".

You shouldn't worry about implementation details, but I think one object is created (I might be mistaken) which incorporates all the superclasses of an object.

And welcome to the Ranch
+Pie Number of slices to send: Send
Is object of superclass created when an object of a subclass created?

Please answer this question clearly
+Pie Number of slices to send: Send
It is created right before the object of the subclass is created. In Java the superclass's constructor is executed right before the subclasses constructor.

Copy and run this code and look at what it does:

(Note: you need 3 separate .java files for this example, but it should clearly show you what happens)

Hope this helps,
Hunter
I can't renounce my name. It's on all my stationery! And hinted in this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 909 times.
Similar Threads
Static methods and inheritance.
Getter / Setter inheritance
Confusion in Method Overriding.
How to prevent base class constructor from executing
Overriding/overloading
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 03:57:49.