• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Inheritance/ Polymorphism question

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a Parent class and it's subclass, Child. I create an instance of Child which inherits methodA() from Parent. When Child is instantiated is Parent also? I would assume so, because Child doesn't have a "copy" of methodA() it uses Parent's methodA(). Is that correct? Going further, if I set my reference to Child to null are both objects available for garbage collection?
Thanks...
 
Ranch Hand
Posts: 8944
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

When Child is instantiated is Parent also?


When you create an child class there will a single child object which inherits the parent class.
 
E Weibust
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to clairfy. You are saying that only one object is created? If so, does that object have the methods "added" to it that it inherited from it's parent? For some reason I thought if I created an instance of Child, I also got an instance of Parent.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You get only one object, which has all the fields and methods from both it's class and all it's superclasses.
 
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by E Weibust:
Just to clairfy. You are saying that only one object is created? If so, does that object have the methods "added" to it that it inherited from it's parent? For some reason I thought if I created an instance of Child, I also got an instance of Parent.


Suppose a father has a car and he has the right to use it. And his son, of course derived from the father, has the right to use that car(if the father let him use == access modifier), even though the car is not owned by the son. The object son exists as only one instance and the object father exists as only one instance also...
Hope it helps...
 
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You get 3 objects.
1. class object for "class Parent"
2. class object for "class Child"
3. instance object for instance of Child.
When you are done using your instance of Child, the JVM will at its discresion dump the 2 class objects. But I am not sure these days. I think the jvm tends to not dump class objects readily because that eliminates any static variables that may have been around. Probably a JVM switch for the garbage collector will dictate the behavior.
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nowadays, classes only get garbage collected when their classloader gets gc'ed. If I remember correctly this behaviour got introduced with JDK 1.2.
 
reply
    Bookmark Topic Watch Topic
  • New Topic