Disabling inherited method on derived class

后端 未结 10 1722
野趣味
野趣味 2020-12-03 06:59

Is there any way to, in a Java derived class, \"disable\" a method and/or field that is otherwise inherited from a base class?

For example, say you have a Shap

10条回答
  •  北海茫月
    2020-12-03 07:22

    I needed the same thing for a custom alertdialog. I needed to pass an argument that became available only at the show moment, so I implemented my show(argument) method, but show() worked too because of the superclass. There was a risk of that I could invoke myDlg.show() by accident, without an argument, and that would cause a crash. To make sure the superclass method is never called outside my CustomAlertDialog class, I added the method private static void show(){} to it. That's it.

    So, to disable a supermethod, make it private and static in the subclass. In your case, it would be:

     private void rotate(){}
    

提交回复
热议问题