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

question from mock exam

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
can any one explain me the following code. is this the example for overloading or overriding?
class Base{
public void amethod(int i) { }
}
public class Scope extends Base{
public static void main(String argv[]){
}
//Method Here
}
1) void amethod(int i) throws Exception {}
2) void amethod(long i)throws Exception {}
3) void amethod(long i){}
4) public void amethod(int i) throws Exception {}

answer is 2,3
my question is overloading mathod is in the same class & overriding method in different class.
but here answer 2&3 which means overloading mathod in another class.can we do that? i was under impression that overloading method only occurs in same class.
can any one explain me?
Thanks
Ketu
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sub class scope will inherit the method amethod(int i){} from the super class Base (You can override it if you want but it is not required) Therefore Scope will always have amethod(int i) as its member. Now Scope can further have its own methods with same name but different input argument(s). Answer 2 and 3 will cause overloading of amethod in Scope class. There is no rule that you can not have new methods of same name in the subclass with different argument(s). Again you are doing overloading in Scope which just happened to be a sub class of Base.Now Base calss has nothing to do with this overloading. These methods only belong to Scope class.Hope I have cleared your point.
[This message has been edited by rkapoor (edited August 07, 2000).]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic