Consider the following code snippets:
package vehicle;
public abstract class AbstractVehicle {
protected int speedFactor() {
return 5;
}
}
package car;
import vehicle.AbstractVehicle;
public class SedanCar extends AbstractVehicle {
public static void main(String[] args) {
SedanCar sedan = new SedanCar();
sedan
.speedFactor();
AbstractVehicle vehicle = new SedanCar();
// vehicle //WON'T compile
// .speedFactor();
}
}
SedanCar
is a subclass of AbstractVehicle
which contains a protected
method speedFactor
. I am able to call the method speedFactor
if it is referenced by the same class. When the super class is used for reference the method speedFactor
is not accessible.
What is reason for hiding the method?