• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Overriding?

 
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Generic class declares the following method to return a float value.

public float method( float fValue )

A junior programmer writing a sub class, which extends Generic, proposes to declare the following overriding method :

public double method( float dValue )

What result do you predict?

A1 It won't compile because of the different return type.
A2 It won't compile because of the different name in the parameter list.
A3 It will compile but will be incompatible with other uses of the method because you can't cast the double return type to float.
A4 It will compile and be compatible with other uses of the calories method.


Answere given is A2.

I think here the name of the parameter to the method "method" is different in the overriding method. Is this allowed for overriding methods? Does the signature also include the parameter name, or just the type and sequence of the parameters?

IOW, is the answer correct?

(tidied up)
[ December 07, 2004: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer is A1, the return type alone cannot be different when you redefine a method in the subclass. For overriding, both return type and parameter list must be the same. For overloading, the return type may or may not differ, but the paramter list must be different.

Parameter names do not matter, only types matter. So A2 is incorrect.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where did you find the question? If it's from a book, did you check the Errata, if any?
 
Kedar Dravid
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not sure whether this link appeared today on Javaranch's SCJP forum. Anyway, the link is http://www.geocities.com/sunjava_scjp (Dec 07, 2004).
By the way, this Question of the Day link appears quite frequently on this forum.
[ December 07, 2004: Message edited by: Kedar Dravid ]
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kedar,
I agree with you....do you smell something fishy about this whole thing??
reply
    Bookmark Topic Watch Topic
  • New Topic