• 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
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

about overloading

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
There's a question from a mock exam..
Consider the following code and select the most appropriate statements.
Public class overL{
public int doubleValue(int a){}
public float doubleValue(int a){}
}

the correct answers given by the exam are
1-The code doesn't compile because the compiler sees two methods with the same signature.
2-The code can be made to compile by redefining the parameter for doubleVal() on line 7 to be "float a" instead of "int a".
I suspect that answer 1 is not correct instead of 'singnature' it should be 'parameter'.
Please someone assure me I'm right.
Thanx
Hina.
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think answer 1 is fine.
The signature of a method is composed by its name and its parameter list (here DoubleValue(int a)). The compiler complains because it indeed sees two methods with the same signature.
Changing only the return value is not enough to overload a method. Changing its parameter list (with float in this case) would make the code compile.

Originally posted by Hina Mustafa:
hi,
There's a question from a mock exam..
Consider the following code and select the most appropriate statements.
Public class overL{
public int doubleValue(int a){}
public float doubleValue(int a){}
}

the correct answers given by the exam are
1-The code doesn't compile because the compiler sees two methods with the same signature.
2-The code can be made to compile by redefining the parameter for doubleVal() on line 7 to be "float a" instead of "int a".
I suspect that answer 1 is not correct instead of 'singnature' it should be 'parameter'.
Please someone assure me I'm right.
Thanx
Hina.


 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
By "Signature" we mean only the Name of the method and the parameter types. The return values are not taken into account.
Question:
1.public int doubleValue(int a){}
2.public float doubleValue(int a){}
In the above example, signature refers to "the method name "doubleValue" and the parameter type "int".
Note: The parameter can be anything i.e. a or b etc.
Hence both the answers choices are correct.
1.The first answer reflects two methods with same signature and so a compil-time error is thrown as the compiler detects a duplicate method.
2.The second answer changes the parameter type from "int" to "float" and thereby makes the second method Overload the first one.
- Suresh Selvaraj
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Hina,
Over loaded methods must differ in the type and/or number of their parameters. The return type alone is not enough to distinguish the two methods.
Choice 1 is absolutely correct.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic