• 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

Jval Test : Overriding & Overloading

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there
I have so doubts about the following question
When can we use same method name in java for two different methods
A. If the two methods are in two unrelated classes.
B. If the two methods are in the same class and they differ in argument & return type.
C. If one method is in base class, other is in its subclass and the two methods differ only in return type.
D. If one method is in base class, other is in its sub class and the two methods differ both arguments list and in return type.
The answer given is A, B and D.
IMHO, when we want to use same method name for two methods it means we want to do overloading. According to my knowledge return type is not part of a method signature. Thus it is not taken account when a method is overload.
As such, statement B is not entirely correct becuase it is possible to differ in argument type only. Am I right?? Are A and D the best answer for this question.
Thanks a lot.
 
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Karen
If you're not sure about answer, you can write a little code and test it.
Answer B says that, the code below will compile.
***************************************
class Test{
int method(int i) { return 0; }
String method(String i) { return ""; }
}
***************************************
It compiles fine and therefore B is true.
Explanation:
This is a sample of method overloding.
Jamal Hasanov
www.j-think.com
 
Karen Leoh
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jamal,
I did try out some samples. I know the example you given compile fine. But want I am dubious over is the usage of the word and in answer B.
I think it's not true that to overload a method we have to differ in return type as return type is not part of a signature.
Examples shown below compile fine too and it's a valid form of overloading.
int MyMethod(int a){return 0;}
int MyMethod(String b){return 0;}
That's the reason why I post the question becuase I want to know the best answer.
Thanks
 
Jamal Hasanov
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
8.4.7 Overloading
If two methods of a class (whether both declared in the same class, or both inherited by a class, or one declared and one inherited) have the same name but different signatures, then the method name is said to be overloaded. This fact causes no difficulty and never of itself results in a compile-time error. There is no required relationship between the return types or between the throws clauses of two methods with the same name but different signatures.

In other words, definition of two methods inside 1 class, with the same name but with different return and argument type(different signature) - is called overloading.
That's why B is true.
Jamal Hasanov
www.j-think.com
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Karen,
The question asks you when you can use the same name for two different methods. Answer (B) is correct because you can indeed use the same name in this instance. It is not asking the definition of overloading/overriding.
 
reply
    Bookmark Topic Watch Topic
  • New Topic