Stopping inheritance without using final

前端 未结 8 1558
攒了一身酷
攒了一身酷 2020-12-14 12:51

Is there any other method of stopping inheritance of a class apart from declaring it as final or by declaring its constructor as private?

8条回答
  •  無奈伤痛
    2020-12-14 13:41

    Using final is the canonical way.

    public final class FinalClass {
      // Class definition
    }
    

    If you want to prevent individual methods from being overridden, you can declare them as final instead. (I'm just guessing here, as to why you would want to avoid making the whole class final.)

提交回复
热议问题