Is there any way to NOT implement all of the methods of an interface in an inheriting class?
If you don't implement all methods of your interface, than you destroy the entire purpose of an interface.
A class must implement all methods of the interface in its implementation part, with the following exceptions: Interface methods declared as optional using the addition DEFAULT.
However, you can avoid this by following the below approach: Declare the missing methods abstract in your class. This forces you to declare your class abstract and, as a result, forces you to subclass the class (and implement the missing methods) before you can create any objects.
Yes, we can implement interface methods without using implements keyword.
The only way around this is to declare your class as abstract
and leave it to a subclass to implement the missing methods. But ultimately, someone in the chain has to implement it to meet the interface contract. If you truly do not need a particular method, you can implement it and then either return
or throw some variety of NotImplementedException
, whichever is more appropriate in your case.
The Interface could also specify some methods as 'default' and provide the corresponding method implementation within the Interface definition (https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html). These 'default' methods need not be mentioned while implementing the Interface.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With