-2
  @FunctionalInterface  
  interface Superman {

    //public methods in java.lang.Object class
    String toString();
    int hashCode();
    boolean equals(Object obj);

    //Protected method in java.lang.Object class
    //Object clone() throws CloneNotSupportedException;
    void finalize() throws Throwable;
}

SAM = (Single Abstract Method)

My Question is @FunctionalInterface does not count public methods as a SAM but protected methods count as a SAM Why...?

Ng Sharma
  • 2,072
  • 8
  • 27
  • 49
  • sorry typo mistake. – Ng Sharma Dec 28 '19 at 12:53
  • Please explain what SAM means. – Michele Dorigatti Dec 28 '19 at 12:57
  • An interface defines public methods. So if you define hashCode(), you're not adding any additional public methods: all objects already have a public hashCode() method. If you define finalize() or clone(), you **add** a new public method: there isn't such a public method in Object. – JB Nizet Dec 28 '19 at 12:57
  • SAM is Single Abstract method @MicheleDorigatti – Ng Sharma Dec 28 '19 at 12:58
  • Thank you Ng Sharma but please edit your question as well, so all can benefit from it. – Michele Dorigatti Dec 28 '19 at 12:58
  • @MicheleDorigatti SAM is a pretty well known acronym in Java world, similar to the likes of POJO. – Adwait Kumar Dec 28 '19 at 13:00
  • See the anser from Adwait Kumar. I don't know which documentation you're talking about. The JLS is, ultimately, what matters. Maybe your documentation made a shortcut and forgot the "public" keyword. What matters for a functional interface is that you must be able to write it as a lambda. If finalize or count() didn't count, how could you possibly create an instance of an interface defining finalize() and another method as a lambda? – JB Nizet Dec 28 '19 at 13:05

1 Answers1

1

This is specified in the JLS.

The definition of functional interface excludes methods in an interface that are also public methods in Object. This is to allow functional treatment of an interface like java.util.Comparator that declares multiple abstract methods of which only one is really "new" - int compare(T,T). The other method - boolean equals(Object) - is an explicit declaration of an abstract method that would otherwise be implicitly declared, and will be automatically implemented by every class that implements the interface.

Note that if non-public methods of Object, such as clone(), are declared in an interface, they are not automatically implemented by every class that implements the interface. The implementation inherited from Object is protected while the interface method is necessarily public. The only way to implement such an interface would be for a class to override the non-public Object method with a public method.

Interfaces don't have clone() and finalize() methods implicitly, so in the case of the interface, that is a new method, so it can't be considered to override anything.

In such a case the implementing classes would have to overridde specifically clone() and finalize(), otherwise it is a syntax error because they have protected visibility and they are claiming to implement a method inherited from the interface with public visibility.

Community
  • 1
  • 1
Adwait Kumar
  • 1,552
  • 10
  • 25