This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "question from mock exam" Watch "question from mock exam" New topic
Author

question from mock exam

K2 Joshi
Greenhorn

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

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).]
 
 
subject: question from mock exam
 
Similar Threads
2 questions
overriding
Overriding a Method
override and overload method
Marcus Green - Qn : 23 - Doubt