• Post Reply Bookmark Topic Watch Topic
  • New Topic

inheritance  RSS feed

 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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?
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abalfazl : Your question is not very clear to me. Can you say more?

Jim ... ...
 
Harsh Kamat
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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/
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is object of superclass created when an object of a subclass created?

Please answer this question clearly
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!