| Java FAQ | ||
| JSP FAQ | ||
| Servlet FAQ | ||
XyzWs Java FAQ:
What happens if a class doesn't implement all the methods defined in the interface?
Printer-friendly version |
Mail this to a friend
|
Advertisement
|
What happens if a class doesn't implement all the methods defined in the interface?When a class is defined to implement an interface, the class must provide definitions of all the methods defined in the interface. Otherwise, you will get compiler time error. A class must implement all methods of the interface, unless the class is declared as abstract. There are only two choices:
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();
}
}
The above code shows a concrete class
|