Win a copy of Fixing your Scrum this week in the Agile forum!
  • 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
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

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.

 
If you open the box, you will find Heisenberg strangling Shrodenger's cat. And waving this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic