Saturday, 27 February 2016

Background

Based on our knowledge till Java 7 we know that methods in an interface are by default public and abstract and that you cannot have a non abstract method in interface. However this does not hold true from Java 8 with the introduction of default methods. You can declare a method default and provide it's body in the interface itself and you need not implement it in the classes that implement corresponding interface (You can definitely override it though).

NOTE : If an interface has no direct superinterfaces, then the interface implicitly declares a public abstract member method m with signature s, return type r, and throws clause t corresponding to each public instance method m with signature s, return type r, and throws clause t declared in Object, unless an abstract method with the same signature, same return type, and a compatible throws clause is explicitly declared by the interface. (JLS)
Though you can specify methods from Object in an interface you cannot override them. For eg. -