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

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.
 
30 seconds to difuse a loaf of bread ... here, use this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic