aspose file tools*
The moose likes Java in General and the fly likes What happens to parent class object when a child class's object is created? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "What happens to parent class object when a child class Watch "What happens to parent class object when a child class New topic
Author

What happens to parent class object when a child class's object is created?

Sumeet H Singh
Greenhorn

Joined: Jan 03, 2009
Posts: 29
Hi all,
I had a doubt regarding creation of object in relation to inheritence.
Whenever you instantiate a Child class object, the parent class's contructor is also called. In other words, in addition to creating the child's object we are also creating the parent's object(let me know if there is a fallacy in this assumption).

My question is that since we actually use only the child class's object, what happens to the parent class's object(is it garbage collected??), and why is it created in the first place?


Sumeet
SCJP, SCWCD
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

There's only one object created. (which should answer your questions )


[My Blog]
All roads lead to JavaRanch
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14347
    
  22

A side note: You should not use the words "parent class" and "child class".

The words "parent" and "child" are not commonly used when talking about inheritance in the object oriented programming sense. Note that the biological meaning of the word "inheritance" is very different from the meaning of the term "inheritance" in OO programming.

In OO programming, "inheritance" indicates an is a relationship: if class Dog extends Animal, then an instance of Dog is an instance of Animal. Note how this would sound wrong if I would have class Child extends Parent: a Child is a Parent?

So, don't use those terms: say "superclass" instead of "parent class" and "subclass" instead of "child class".

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Jesper Young wrote:A side note: You should not use the words "parent class" and "child class".

I think this is a bit subjective. The Java tutorial also uses the parent/child wording and I think it is widely used :
Definitions:
A class that is derived from another class is called a subclass (also a derived class, extended class, or child class). The class from which the subclass is derived is called a superclass (also a base class or a parent class).
Sumeet H Singh
Greenhorn

Joined: Jan 03, 2009
Posts: 29
Thanks Christophe for the answer
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14347
    
  22

Christophe Verré wrote:
Jesper Young wrote:A side note: You should not use the words "parent class" and "child class".

I think this is a bit subjective.

In my opinion, confusing the biological meaning of the word "inheritance" with its meaning in OO programming makes it harder for people to understand what "inheritance" means in OO programming.

When you're talking about a "parent class" and "child class", it's harder to understand that there's an is a relationship between those two (which is the essential point in understanding inheritance in OO programming). That's why I want to discourage people to use words that refer to the biological meaning of the word.
Vinod Vinu
Ranch Hand

Joined: Aug 30, 2009
Posts: 217
give me the answer


Vinod Kumar Nair
"Any fool can write code that a computer can understan. Good programmers write code that humans can understand."
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

That's a bit rude, isn't it? Demanding the answer to someone else's question while Christophe already answered it in the first reply.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Vinod Vinu
Ranch Hand

Joined: Aug 30, 2009
Posts: 217
I'm sorry
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Jesper Young wrote:That's why I want to discourage people to use words that refer to the biological meaning of the word.

Thank you Jesper. I understand why you want to discourage the use of these words
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11479
    
  16

Chetan Dorle,
Your post was moved to a new topic.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What happens to parent class object when a child class's object is created?