File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

question from mock exam

 
K2 Joshi
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
rkapoor
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • 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.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic