• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Methods overload or override ?

 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was confused , I thought when I posted my response. How come it deleted yours ?
 
Bras cause cancer. And tiny ads:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic