3

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?

Ankit_ceo2
  • 317
  • 1
  • 6
  • 14
  • Seems it was a trick question. – ernest_k Jun 21 '19 at 19:06
  • 1
    Functional interface can have any number of default methods. So it's not a marker interface at all. – PM 77-1 Jun 21 '19 at 19:07
  • 1
    @PM77-1 And more specifically, functional interfaces need to be SAM types (single abstract method), and the common convention seems that a marker interface has neither constants nor methods. – GhostCat Jun 21 '19 at 19:14
  • 2
    I'd argue functional interfaces are the opposite of marker interfaces: for marker interfaces, you need to declare the interface but no method; for functional interfaces, you don't need to declare the interface but have a method. – daniu Jun 21 '19 at 19:24

3 Answers3

7

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.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • I would like to add that, at least in my book, a marker interface is always a smell. If you need a marker, use an annotation instead. The most famous misuse of a marker interface is `Cloneable`. – Turing85 Jun 21 '19 at 19:25
  • @Turing85 can you explain "misuse of cloneable" in some more details please. – Ankit_ceo2 Jun 21 '19 at 19:29
  • 1
    What do you mean doesn't have an official definition? A marker interface is an interface declaring no methods, a functional interface is one with only one abstract method. It may not be defined in the language spec, but I think it's part of the OCP IIRC, so official canon (arguably) . – daniu Jun 21 '19 at 19:29
  • 4
    @Turing85 I would say that "marker interface" are relicts of a time before there were annotations... but I dont fully agree: annotations require the framework to use reflection. We have a few places with our own marker interfaces, and being able to do instanceof-checks is a much better "usage concept" than reflection. – GhostCat Jun 21 '19 at 19:31
  • @daniu [Copy Constructor versus Cloning (an interview with Josh Bloch](https://www.artima.com/intv/bloch13.html) – Turing85 Jun 21 '19 at 19:32
  • 3
    @daniu When talking to 5 different groups in our org, I am sure I would come back with 5 different definitions of the term "unit test". That taught me one important lesson: unless terms are spec'ed and carved in stone: don't expect that everybody agrees to your definition ... – GhostCat Jun 21 '19 at 19:33
  • I'm not arguing that definitions are a good idea, I'm saying they exist for both terms. – daniu Jun 21 '19 at 19:49
  • there's always a struggle when to use a marker interface vs an annotation; for me personally. still can't tell – Eugene Jun 21 '19 at 20:39
  • I don’t know whether `instanceof` is so much better than Reflection, as far as annotations are involved. Annotations have to advantage that you can deliberately decide, whether subclasses will inherit them automatically, unlike marker interfaces which always pollute entire class trees. On the other hand, when you want to mark a lambda expression, you can only use marker interfaces… – Holger Jun 24 '19 at 12:04
0

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.

Bipil Raut
  • 244
  • 1
  • 10
0

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.

senjin.hajrulahovic
  • 2,961
  • 2
  • 17
  • 32