wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Methods overload or override ? 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 "Methods overload or override ?" Watch "Methods overload or override ?" New topic
Author

Methods overload or override ?

Kay Liew
Ranch Hand

Joined: Dec 26, 2003
Posts: 112
Confused � Please see my comments

Question # 7 from KB book
Given the following,

Choice

A � String doStuff(int x){return �hello�;} //identical to the parent method � good for me

B � int doStuff(int x) {return 42;} // first look, it was fine then the argument didn�t change while the return type has changed � violated the overloading rules # 2 (Page 333 Kathy and Bert)

C � public String doStuff(int x){return �Hello�;} //identical to the parent method except it has higher access modifier. Good for me.

D � protected String doStuff(int x){return �Hello�;} //now this one contradicted with Kathy and Bert book page 332 � Sun Obj 6.2) bullets 3 and 4. They are ..

Bullet # 3:Must not have a more restrictive access modifier
Bullet # 4:May have a less restrictive access modifier

E - String doStuff(String s){return �Hello�;} //argument list changed and return type remained the same. Is this override or overload ? Wouldn�t this violates overloading rules # 2 (Page 333 KB book)?

F � int doStuff(String s){return 42;} //because of B is incorrect and F is correct � same concept.

In general, while this question has only given 1 line of code how do I know if it�s overridden or overloading type of question ??? Can I assume from A-F are ONLY referring to overloading topic ?




Thanks.
kay

[ October 28, 2004: Message edited by: Kay Liew ]
[ October 28, 2004: Message edited by: Kay Liew ]

Unity can only be manifested by the Binary. Unity itself and the idea of Unity are already two.
Jay Pawar
Ranch Hand

Joined: Aug 27, 2004
Posts: 411
Hi Kay,
Hope this helps ya.


Choice A is legal override because the access modifier is same, the function signature is same and so is the return type

Choice B is illegal WHY ?

First let us analyze from Overriding point of view
Is the function signature along with the return type is same --> NO

Now from overloading point of view
Is the function signature different ? ---> NO because the parameter list is same.

Choice C is legal override because function signature is same along with the return type and access modifier is less restrictive.

Choice D is legal override reason same as Choice C ( note: Access modifier is protected which is again less restrictive than package access ( default access ).

Choice E is legal overload because the paramater list is changed. Overload doesn't care about return type.

Choice F is legal overload reason same as Choice E.


Cheers,<br />Jay<br /> <br />(SCJP 1.4)<br />Heights of great men were not achieved in one day, they were toiling day and night while their companions slept.
Jay Pawar
Ranch Hand

Joined: Aug 27, 2004
Posts: 411
Package level access is default access


In the above code instance variable int four has package level access ( default access )
Kay Liew
Ranch Hand

Joined: Dec 26, 2003
Posts: 112
Jay, thanks again,.. Sorry that i have deleted my previous response because i have found out the default access's level. Your responses are all helped

k
Jay Pawar
Ranch Hand

Joined: Aug 27, 2004
Posts: 411
I was confused , I thought when I posted my response. How come it deleted yours ?
 
jQuery in Action, 2nd edition
 
subject: Methods overload or override ?
 
Similar Threads
Constructor overloading problem
Inner Class explaination?
Advanced Overloading (widening,boxing,var args)
JQuiz - Practice Questions for SCJP2 (10/19/2003)
S&B Practice Exams, Test 1, # 14 question