• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

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

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's only one object created. (which should answer your questions )
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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".
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Christophe for the answer
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
give me the answer
 
Sheriff
Posts: 22504
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Vinod Vinu
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm sorry
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
lowercase baba
Posts: 13013
66
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Chetan Dorle,
Your post was moved to a new topic.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic