• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

inheritance

 
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?
 
Ranch Hand
Posts: 492
Firefox Browser VI Editor Linux
  • 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
 
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 ... ...
 
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?
 
Marshal
Posts: 68964
275
  • 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 VI Editor Linux
  • 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
 
Being a smart alec beats the alternative. This tiny ad knows what I'm talking about:
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic