Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in method call

 
Mahmoud Metwaly
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65111
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"mahmoud.metwally", please check your private messages for an important administrative matter.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Siva Sekhar
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Class Test does not seem to have a constructor that takes an integer. Was this all the code given?
 
Raphael Rabadan
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Sandeep Bhandari
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mahmoud Metwaly must have missed that one argument constructor.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic