This week's book giveaway is in the Java in General forum.
We're giving away four copies of Helidon Revealed: A Practical Guide to Oracle’s Microservices Framework and have Michael Redlich on-line!
See this thread for details.
  • 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

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.

 
Time is the best teacher, but unfortunately, it kills all of its students - Robin Williams. tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic