File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Methods overload or override ?

 
Kay Liew
Ranch Hand
Posts: 112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Jay Pawar
Ranch Hand
Posts: 411
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jay Pawar
Ranch Hand
Posts: 411
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Package level access is default access


In the above code instance variable int four has package level access ( default access )
 
Kay Liew
Ranch Hand
Posts: 112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 411
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was confused , I thought when I posted my response. How come it deleted yours ?
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic