aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes difference between parent object ref and child object ref Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "difference between parent object ref and child object ref" Watch "difference between parent object ref and child object ref" New topic
Author

difference between parent object ref and child object ref

Kicky San
Greenhorn

Joined: Aug 24, 2005
Posts: 18
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


Cheers,<br />KicKy
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

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

Joined: Aug 22, 2005
Posts: 193
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


Rational Pi Blog - Java, SCJP, Dev Bits- http://rationalpi.wordpress.com
Kicky San
Greenhorn

Joined: Aug 24, 2005
Posts: 18
Joshua and Steve,
I would like to thank you both. I better understood about the references and objects after reading the article.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: difference between parent object ref and child object ref