Why protected method is not accessible from subclass?

后端 未结 5 1422
我在风中等你
我在风中等你 2021-02-08 23:04

Consider the following code snippets:

package vehicle;

public abstract class AbstractVehicle {
    protected int speedFactor() {
        return 5;
    }
}

pack         


        
5条回答
  •  不要未来只要你来
    2021-02-09 00:05

    The protected modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package.

    This is the reason why you can't directly call the method inside the main method on the vehicle object.

    See: https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

提交回复
热议问题