• Post Reply Bookmark Topic Watch Topic
  • New Topic

Inheritance/ Polymorphism question  RSS feed

 
E Weibust
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • 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...
 
Pradeep bhatt
Ranch Hand
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • 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.
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
  • 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...
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!