Java: how to implement private abstract methods?

后端 未结 2 1625
隐瞒了意图╮
隐瞒了意图╮ 2020-12-30 20:30

Is it possible to define a private abstract class in Java? How would a Java developer write a construct like below?

public abstract class MyCommand {
    pub         


        
相关标签:
2条回答
  • 2020-12-30 21:15

    You can't have private abstract methods in Java.

    When a method is private, the sub classes can't access it, hence they can't override it.

    If you want a similar behavior you'll need protected abstract method.

    It is a compile-time error if a method declaration that contains the keyword abstract also contains any one of the keywords private, static, final, native, strictfp, or synchronized.

    And

    It would be impossible for a subclass to implement a private abstract method, because private methods are not inherited by subclasses; therefore such a method could never be used.


    Resources :

    • JLS - 8.4.3. Method Modifiers
    • JLS - 8.4.3.1. abstract Methods
    0 讨论(0)
  • 2020-12-30 21:30

    That would be protected instead of private. It means that only classes that extend MyCommand have access to the two methods. (So do all classes from the same package, but that's a minor point.)

    0 讨论(0)
提交回复
热议问题