File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Reference variable casting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Reference variable casting" Watch "Reference variable casting" New topic
Author

Reference variable casting

Chetan Dodiya
Ranch Hand

Joined: Jun 27, 2008
Posts: 39
if



at line 1 2 & 3 which methods are called means Dog's class And Animal's class???


SCJP 1.5
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

All those calls will invoke the method from the DOG class.

1.Because its overridden.
2.the object is actually a Dog class object and at this point the same object is actually refered by the Dog class reference
3.And again casting (for the time being)
[ November 14, 2008: Message edited by: James Tharakan ]

SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
Chetan Dodiya
Ranch Hand

Joined: Jun 27, 2008
Posts: 39
thank you very much....
[ November 14, 2008: Message edited by: Chetan dodiya ]
marthi reddy
Greenhorn

Joined: Oct 27, 2008
Posts: 6
Remember this rule, When a method is overridden(thru inheritance), then during run time, depending on the object type that the referrence variable is pointing to, the correspoding type's method will be invoked.

In the current example, ref variables a, d and the casted a(line 3) are all pointing to the Dog Object , doStuff() method is overridden and hence the doStuff() method in Dog Class will be invoked.


I belive in "Judge your Java concepts by taking SCJP exam". <br /> <br />Happy preparation for SCJP 6.0
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reference variable casting