Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

overloading + inheritance + widening

 
Akila Sathyan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
came across this code in a mock exam.
----------------



-----------
Since the parameter to the method "scale()" was '1' - an int literal, I assumed that the method in the Circle class will be invoked. But the method in the base class Shape gets invoked. Can someone please explain?

[edit]Add code tags. CR[/edit]
[ October 04, 2008: Message edited by: Campbell Ritchie ]
 
Henry Wong
author
Marshal
Pie
Posts: 22120
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
came across this code in a mock exam.


Also... Please Quote Your Sources.

Thanks,
Henry
 
Henry Wong
author
Marshal
Pie
Posts: 22120
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since the parameter to the method "scale()" was '1' - an int literal, I assumed that the method in the Circle class will be invoked. But the method in the base class Shape gets invoked. Can someone please explain?


The reference type is of Shape. The method (signature of) that it will call -- including which overloaded method, is determined at compile time, using the reference type. Based on the reference, there is only one scale() method, which takes a long, hence, that is the method that is called.

Henry
[ October 04, 2008: Message edited by: Henry Wong ]
 
Akila Sathyan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry about the protocol goof-ups. I've updated my profile (I did have to give up a little bit of my culture - I'm from I-only-got-one-name culture!)

Here is the mock exam link:
http://www.irixtech.com/category/java/scjp-quiz
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic