Java: Force subclasses to override methods of the Superclass
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
:
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
.
精彩评论