aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in method call 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 "Doubt in method call" Watch "Doubt in method call" New topic
Author

Doubt in method call

Mahmoud Metwaly
Greenhorn

Joined: Jul 16, 2008
Posts: 14
I've found the following question in one of the mock up exams where the correct answer is strange for me



Now the correct answer for this question is that it Prints 5
while I think that it should Prints 0 since the method called during runtime is the method of the reference type (SuperTest) here

So am I thinking correct here or am I missing something?

Thanks in advance for your help


Mahmoud Metwally<br />Preparing for SCJP...<br /> <br />"Try not to become a man of success but a man of value" - Albert Einstein
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60053
    
  65

"mahmoud.metwally", please check your private messages for an important administrative matter.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by mahmoud.metwally:
...I think that it should Prints 0 since the method called during runtime is the method of the reference type (SuperTest) here...

It's the runtime type of the object (not the reference) that determines the instance method invoked. See How my dog learned polymorphism.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Siva Sekhar
Ranch Hand

Joined: Jun 11, 2008
Posts: 37
HI...mahmoud.metwally

Calling of methods depends upon the Object type but not reference.
But when it comes to varaibles then it depends upon reference(not the object) type
Ronald Schild
Ranch Hand

Joined: Jun 09, 2008
Posts: 117
Class Test does not seem to have a constructor that takes an integer. Was this all the code given?


Java hobbyist.
Raphael Rabadan
Ranch Hand

Joined: Jul 05, 2008
Posts: 141
Originally posted by Ronald Schild:
Class Test does not seem to have a constructor that takes an integer. Was this all the code given?


Was thinking the same thing...


SCJP Java 6 (98%) - Story, SCJA (88%) - Story
Sandeep Bhandari
Ranch Hand

Joined: Apr 16, 2004
Posts: 201
Mahmoud Metwaly must have missed that one argument constructor.


SCJP 96% | SCWCD 90%| SCJP mock exams | My SCJP blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in method call
 
Similar Threads
Exception in constructor
Overloading or Overriding ?
Constructor Problem
this(i)
Dynamic polymorphism and Overriding