Win a copy of Event Streams in Action this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Doubt in method call

 
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
 
Marshal
Posts: 67273
170
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"mahmoud.metwally", please check your private messages for an important administrative matter.
 
Sheriff
Posts: 11343
Mac Safari Java
  • 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.
 
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
 
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?
 
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...
 
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mahmoud Metwaly must have missed that one argument constructor.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!