Implicit method declaration in Java Interfaces


Ever noted that in the Comparator<T> interface we have two abstract methods:

·         static compare(T o1, T o2);
·         boolean equals(Object obj);

Yet the interface is declared as a functional interface. We can create an instance of the Comparator<T> even without extending the method “equals”, because all classes the methods in the class Object  is declared implicitly in an interface, and the method “equals” is the member of the class “Object”. If you choose you can override it or else leave it. You would always have a concrete implementation of the method “equals”. The question now arises, why was it declared in the Comparator <T> interface in the first place? Logically speaking there was no need to do so. The only reason it is there is for documentation inside the javadoc so that you know that equals forms an integral part of the Comparator interface. Nothing else. If you do not override, there is no harm done.

Let us see a point in the interface member declaration from Java:
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.

This actually says that if there are no direct superinterfaces for an interface, then all the methods of the class Object are declared abstract implicitly in the interface. This is done so that if you take a reference as that of the interface you could still invoke the methods of Object class such as “equals or “toString or any other. Let us look at the interface declaration below:


public interface MyInterface {
      
       // abstract method
       public void foo();
      
              // methods which have same signature
       // as the methods in the Object class
       boolean equals(Object obj);     
       public String toString();
}


In this interface we have an abstract method “foo and two abstract methods which (have exact signature) belong to the class “Object”. We can only override the method “foo if we so wish, without overriding “equals” and “toString. Since the latter two methods were already implicitly declared we can use them even when we refer to them as the interface.


public class MyInterfaceProcessor implements MyInterface {

       @Override
       public void foo() {       
              // do something
              System.out.println("Printing MyInterface .... !!!");
       }
             
       public static void main(String... args) {
             
              // the processor is taken as MyInterface
              MyInterface mfi = new MyInterfaceProcessor();
             
              // executing foo() method
              mfi.foo();
             
              // it can also call toString() method
              // even if the method is not declared in the
              // MyInterface interface
              System.out.println("To String = " + mfi.toString());
       }
}



Comments

Popular posts from this blog

Java Generics - 7. Upper and Lower Bounds

Java Functional Programming : 3. The java.util.function Library

Java Generics - 3. Multiple bounding