• 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

Overloaded methods

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a small doubt here about the question in Marcus:
Given the following class definition, which of the following methods could be legally placed after the comment

public class Rid{
public void amethod(int i, String s){}
//Here
}
1)public void amethod(String s, int i){}
2)public int amethod(int i, String s){}
3)public void amethod(int i, String mystring){}
4) public void Amethod(int i, String s) {}
The correct answer is 1&4.But since the overloaded can also accept different return type,i suggest that the correct answer is
1&2.Also,overloaded methods have the same name as that of overloading method.Hows the answer 4 correct?Can anyone help me?
Thanks
rubna
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java is case-sensitive.
Therefore, (for instance), 'int max' is not the same as 'int MAX'. Along the same lines, method 'Amethod()' is not the same as 'amethod()'.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rubna,
By just changing the return type the method can't be overloaded..for overloading atleast the argument type/number should be changed.
Aamir.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here are some details.
1)public void amethod(String s, int i){} is legal and a valid overloaded method since the argument types are different.
2)public int amethod(int i, String s){} is illegal because there is no change in the argument type.
You cannot have two methods with the same method name and number and order of arguments and type in a class.
3)public void amethod(int i, String mystring){} is illegal becuase difference in variable name doesnot count to be a valid method.
4) public void Amethod(int i, String s) {} is legal because it is a different method name and so a legal method altogether.
Hope this helps
Thunthu.

 
We can walk to school together. And we can both read this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic