A class must implement all methods of the interface, unless the class is declared as abstract. There are only two choices:
- 1.Implement every method defined by the interface.
- 2.Declare the class as an abstract class, as a result, forces you to subclass the class (and implement the missing methods) before you can create any objects.
The only case the class do not need to implement all methods in the interface is when any class in its inheritance tree has already provided concrete (i.e., non-abstract) method implementations then the subclass is under no obligation to re-implement those methods. The supclass may not implement the interface at all and just method signature is matched. For example,
interface MyInterface { void m() throws NullPointerException; } class SuperClass { //NOTE : SuperClass class doesn't implements MyInterface interface public void m() { System.out.println("Inside SuperClass m()"); } } class SubClass extends SuperClass implements MyInterface { } public class Program { public static void main(String args[]) { SubClass s = new SubClass(); s.m(); } }
No comments:
Post a Comment