• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

overloaded methods

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Given the following class definition, which of the following methods could be legally placed
after the comment
//Here

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) {}
according to me the ans are:1,3
ans given are :1,4
for overloaded methods the method name should be same , the method name in option 4 is Amethod..
i think the 3rd option is correct,it is an overriden method
please explain
thanks
sherin
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sherin,
Ans 3 is incorrect, because you're trying to add a method with the same signature to the same class, although the name of the second parameter is different, but that doesn't count in overloading.
Ans 4 is correct, but is not a case of overloading, because the name of the method is different.
Hope this helps,
Peter
[This message has been edited by Peter Voorwinden (edited September 15, 2000).]
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi..
I feel options 1 and 3 mean the same.In overloaded methods, what matters is only the type and number of arguments passed, hence forget about identifier names and the order.and, you cant place them in your code because you cant have two mehtods with same signature(arguments and return types) in a class..you are confusing the compiler!!
the compiler treats overloaded methods as two different methods.
Option 4 is not an overloaded method because its name is different.
I think here, only option 2 is the answer. since it takes a String and an int as argument and returns an int, i think the compiler treats it as an altogether different method, hence the answer.
point out if I am missing something.
Ramani.
 
sheri
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
option 1 is correct,since the parameters are different..
my question is why option 3 is incorrect,compiler can treat it as an overriden method..
for overloaded method the only thing is its method name should be the same..then how come the ans 4 is correct,since the method name is different...
i think the ans should be 1,3
ans 1 because it is an overloded method
ans 3 because complier treat it as an overriden method,for overriden methods the only rule is that the parameter name should be different

can anybody explain....
thanks
sheri
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sheri,
Ans 4 is correct. Note the question. The question does not ask you which are 'legally overloading' methods. It simply asks you which methods are legal at that point in code. Hence like
public void foo(int i, String s)
would be legal at that point,
even public void Amethod(int i, String s) is also legal.
Ans 3 is incorrect. This is because Java requires that the signatures of method be different. And a method signature includes the name of the method, and the order and data type of the parameters; which means that 1) parameter names are insignificant (2) return types are insignificant. Hence Ans 3 is incorrect.
I hope I have been able to help you.
Regds
Sathish
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey SatishVJ, ans (1) is also correct. In fact u have said the answer u r self by quoting "a method signature ncludes the name of the method, and the order and data type of the parameters"
So,since the order of i/p argumernts are reversed, thereefore ans(1) can be placed over //here. It is another example of overloading.
Therefore my answers would be : (1) and (4)
ans (4) is very obvious.
-sampaths77
 
It's just a flesh wound! Or a tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic