开发者

Java: Force subclasses to override methods of the Superclass

开发者 https://www.devze.com 2023-04-03 22:34 出处:网络
How can I write a method and force the subclasses to override t开发者_如何学Gohis method. In Eclipse it should show in the Quick-Fix Dialog: \"Add unimplemented methods\".

How can I write a method and force the subclasses to override t开发者_如何学Gohis method. In Eclipse it should show in the Quick-Fix Dialog: "Add unimplemented methods".

Thanks


How can I write a method and force the subclasses to override this method.

Declare the method as abstract:

Java: Force subclasses to override methods of the Superclass

Eclipse will give you the "Add unimplemented methods"-option for all (unimplemented) abstract methods and interface methods.


Just declare the method as abstract in the base class. All children classes will then be forced to implement it. Alternatively you could also use an interface instead of a concrete class, which is simply an agreement that the methods defined will be implemented. Either one is fine, it depends on your needs.


You can do that by making the method abstract (not providing a default implementation).


Or if you do NOT want your class to be an abstract class, you can throw your own MustOverrideException at the method that you want to force to be overridden.


Declare the method as abstract.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号