• 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
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Overload

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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) {}
Ans:1)&4)
Question: Is choice 2) is overriding though return type is different ?
 
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
Remeber - You can not override the method in the same class.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
...and you can't override using a different return type. So it's illegal for two reasons.
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here you arent talking of overriding but overloading. Overriding methods is something that can happen only in subclasses of the class concerned. To answer your qs, methods with identical signatures( signatures only include name and args )cannot have different return types. Basically if you think about it, its logical - if such a method is called, how should the decision be made?? you cannot have some calls to a method returning a value and others not. your program is going to fall apart if there is an ambiguity about whether a value will be returned or not. And java doesnt (yet) resolve method calls by looking at the calling statement to see if its expecting a return value or not!!!:-)
tho this does bring a qs to my mind.
Can overloaded methods throw different kinds of exceptions??? or do the exceptions also have to belong to one family???
 
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nalini,
Overloading methods in the same class or subclass are COMPLETELY NEW BORN methods. They just happened to have the same name as the overloaded method. The overloading methods can return any value, throw any kind of Checked Exception/RuntimeException/Error as they wish.
regds
maha anna
 
Nalini Mistry
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i stand corrected. Overloading is when just the names are the same but the arg types and/or nos are different. so the ret value and exceptions can be different. Overriding is when name AND arg list and type is identical, and it cannot happen in the same class. Woz stating in the context of Umesh's qs. In case of option no 2. arent we trying an (illegal) override instead of overload since the method sign is the same??
[This message has been edited by Nalini Mistry (edited March 26, 2000).]
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nalini- right, it's an illegal override rather than an overload.
 
Cob is sand, clay and sometimes straw. This tiny ad is made of cob:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic