Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Overriding  RSS feed

 
Sona Makheeja
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If a base class has a method defined as
void method() { }
Which of the following are legal prototypes in a derived class of this class. Select the two correct answers.
void method() { }
int method() { return 0;}
void method(int i) { }
private void method() { }



what are the correct choices? i read this somewhere n not Ok with the answers, i tried comiling thing too, can anyone explain the choices plz
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The rule to follow is: if the parent and child class's method signatures match - name and formal parameter number and types - then the return type must match and the method's access modifier cannot be more restrictive in the child class. If the child class's method signatures don't match, it's a different method (overloaded) and the other things do not have to match at all.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"GeetaL",

We're a friendly bunch and pretty light in rules here at the JavaRanch, but one that the take quite seriously is the display name rule. You can check it out here.

Basically we require your display name to be two words: your first name, a space, then your last name. Fictitious names are not allowed.

Please edit your profile and update your display name, since accounts with invalid display names get deleted, often without warning.

thanks,
Dave.
 
Hentay Duke
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
void method() { }

int method() { return 0;} //returns a different type

void method(int i) { }

private void method() { } // more restrictive
 
Sona Makheeja
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

...then the return type must match


This 'rule' has changed with the introduction of J2SE 5.0.
Read up on 'co-variant return types' for more information.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!