Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

difference between parent object ref and child object ref

 
Kicky San
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Ranchers,

can anybody explain me the difference between

Parent P = new Parent();
and
Parent P = new Child();


and how the object reference variable 'P' works in each case?(with some examples please)

hope you wont mind this question as too trivial or silly to answer.

thanks in advance,
Karthick
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can anybody explain me the difference between

Parent P = new Parent();
and
Parent P = new Child();


and how the object reference variable 'P' works in each case?
The reference variable works the same in both cases; it contains a value that points to an object on the heap. In the second case, I presume that Child extends Parent, and is therefore a Child object is type-compatible with a reference of the Parent type.

What questions do you have about this?
 
Joshua Smith
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karthick-

Here is an article that may be helpful to you.

How my Dog learned Polymorphism
http://www.javaranch.com/campfire/StoryPoly.jsp

Hope that helps,
Josh
 
Kicky San
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joshua and Steve,
I would like to thank you both. I better understood about the references and objects after reading the article.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic