Search code examples
javafunctional-interface

Funtional Interface


I am new in Functional Interface and today i am learning from few of tutorial sites. I have a question plz provide your suggestions and guide me.

Below mentioned code have an question for me.

@FunctionalInterface
interface Demo {
    Object clone();   // protected
    //int hashCode();              // public
    //boolean equals(Object c); // public
    //public void wait(); // final so we cannot override this one.
}

Object class is parent for all java classes. Here wait() method says not overrided because this one is final. So it means Demo interface also child of Object class (in general terms).

> @FunctionalInterface means interface with exact one method declaration.

Question: So, Now code is working when Object clone(); method is not commented. So means this method is declared in interface Demo. But when we click on its implementation we move on Object class's clone() method.

When we comment clone() method and un-comment equals() method, then we get compile time error, interface is not FunctionalInterface. Why ?????? and why its functional interface with clone() method.

Please don't say clone() is protected, what's the matter if clone is protected in Object class. Please explain for me.

Thanks, sawai


Solution

  • Since public boolean equals(Object c) already exists in Object, Demo doesn't declare any new method. To be a FunctionalInterface, it should declare only one method.

    When instead you declare public Object clone(), it is a new method because the original one in Object is protected. Therefore it can be considered as a FunctionalInterface.