Ankit_ceo2
Ankit_ceo2

Reputation: 317

How/Why can we say that the functional interfaces in java 8 are marker interfaces

In a recent interview I was asked the question that "how can we say that in java8 the functional interfaces are similar to marker interfaces".

I was not able to answer this question.

But I think marker does not even have any methods while functional interface has to have one method to be overridden.

Can someone help me understand this whether this is a valid argument in some context maybe or the question itself is wrong?

Upvotes: 3

Views: 2881

Answers (3)

senjin.hajrulahovic
senjin.hajrulahovic

Reputation: 3191

In order for a functional interface to compile, besides annotating the interface with @FunctionalInterface you have to declare a single abstract method as part of the interface.

If you try to compile an interface annotated with @FunctionalInterface but without an abstract method you'll get:

FunctionalInterfaceAttempt.java:1: error: Unexpected @FunctionalInterface annotation
@FunctionalInterface
^
  JavaJava is not a functional interface
    no abstract method found in interface JavaJava
1 error

and if you try to compile the same intarface with multiple abstract methods you'll get:

JavaJava.java:1: error: Unexpected @FunctionalInterface annotation
@FunctionalInterface
^
  JavaJava is not a functional interface
    multiple non-overriding abstract methods found in interface JavaJava
1 error

I think that such behaviour does not fit the definition of a marker interface.

Upvotes: 0

Bipil Raut
Bipil Raut

Reputation: 262

Some more details : Functional interface:

From Java 8 Docs

public @interface FunctionalInterface An informative annotation type used to indicate that an interface type declaration is intended to be a functional interface as defined by the Java Language Specification. Conceptually, a functional interface has exactly one abstract method. Since default methods have an implementation, they are not abstract. If an interface declares an abstract method overriding one of the public methods of java.lang.Object, that also does not count toward the interface's abstract method count since any implementation of the interface will have an implementation from java.lang.Object or elsewhere. Note that instances of functional interfaces can be created with lambda expressions, method references, or constructor references. If a type is annotated with this annotation type, compilers are required to generate an error message unless: The type is an interface type and not an annotation type, enum, or class. The annotated type satisfies the requirements of a functional interface. However, the compiler will treat any interface meeting the definition of a functional interface as a functional interface regardless of whether or not a FunctionalInterface annotation is present on the interface declaration.

May be your interviewer want info about SAM.

So Functional interface a marker interface at all.

Upvotes: 0

GhostCat
GhostCat

Reputation: 140457

Typically, a marker interface is an interface that alone by its presence has some sort of effect. In other words: some sort of framework will use instanceof or maybe reflection to identify situations where some object or class implements that marker interface, to then do something based on that information.

And I agree to your understanding: calling a specific method of that interface isn't part of that concept "marker interface", at least in my book.

And beyond my book, that seems to be a well known convention: marker interfaces do not declare a method, see here or there. Both these sources emphasize: a marker interface doesn't have methods or constants.

Therefore I agree with your stance: Function and other interfaces in that package aren't marker interfaces in that strict sense.

On the other hand, I doubt that you will find an "official" definition of that term (for example in the Java Language spec). And when there is no official standard, people are free to make up the "meaning" of words.

So maybe your interviewer decided that "being a SAM" interface is somehow a "marker", too. And I am pretty sure you can't sue him for his opinion.

Upvotes: 7

Related Questions