• Post Reply Bookmark Topic Watch Topic
  • New Topic

Visibility of inherited method  RSS feed

 
Edward Lawrence
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given the following superclass:

package food;
public abstract class Fruit {
abstract String spoilRate();
}

And the following class extending it: (I know that Apple will not compile as coded!)

import food.Fruit;
class Apple extends Fruit{

String spoilRate(){
String sr = "spoil rate currently unavailable";
return sr;

}

}

My Question:
Is there another way to make the inherited method 'spoilRate' in Apple visible without doing the following
a. making 'spoilRate' public in both classes
b. moving Apple to the food package
so that Apple will compile?
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No.

Welcome to the Ranch

The idea is that you can override a "visible" instance method. You can't override an "invisible" method; that is equivalent to writing a new method. You can't invoke an "invisible" method. If you want to call it, that would have to be inside a public or protected method in the superclass, which you can use unchanged or overridden in subclasses.
You can't override static members or fields; that would be called called "hiding". We have an FAQ which might be helpful.
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can give the method protected access, which means access in the same package and subclasses.
 
Edward Lawrence
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much for your answer. Very appreciated.
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome (and sorry for delay; I seem to have had a busy weekend).
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!