File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes question from mock exam Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "question from mock exam" Watch "question from mock exam" New topic

question from mock exam

K2 Joshi

Joined: Jul 12, 2000
Posts: 9
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?

Joined: Aug 05, 2000
Posts: 2
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).]
I agree. Here's the link:
subject: question from mock exam
jQuery in Action, 3rd edition