Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can marker interface like serializable contain default methods?

I think it can't, because marker interface principle is to not have any methods, but since default methods are not abstract I am not sure.

like image 949
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa Avatar asked Nov 03 '14 21:11

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa


People also ask

Can marker interface have methods?

A marker interface is an interface that has no methods or constants inside it. It provides run-time type information about objects, so the compiler and JVM have additional information about the object.

How many methods exist in a marker interface?

It has three types: Serializable interface. Cloneable interface. Remote interface.

Why there are no methods in marker interface?

Marker Interfaces in Java have special significance because of the fact that they have no methods declared in them which means that the classes implementing these interfaces don't have to override any of the methods. A few of the marker interfaces already exist in the JDK like Serializable and Cloneable.

Is marker interface Serializable?

The Serializable interface is present in java.io package. It is a marker interface. A Marker Interface does not have any methods and fields. Thus classes implementing it do not have to implement any methods.


3 Answers

A "Marker" interface is just a regular interface as far as Java is concerned. Thus, it can have default methods just as any (Java-8) interface can.

Now, as to whether this violates the principle of a Marker interface, I would have to say yes. A Marker interface should act as a flag of sorts, only identifying that a class meets some external criteria. Now, it can be a Marker interface and have abstract/default methods, but it will no longer purely meet the definition.

From Effective Java (Second Edition):

A marker interface is an interface that contains no method declarations, but merely designates (or “marks”) a class that implements the interface as having some property.

like image 136
Azar Avatar answered Sep 28 '22 08:09

Azar


A Marker Interface is a design pattern, so we can start to answer your question by observing just what the definition is:

In earlier versions of Java, Marker Interfaces were the only way to declare metadata about a class. For example, the Serializable Marker Interface lets the author of a class say that their class will behave correctly when serialized and deserialized.

The purpose of a Marker interface, in the context of Java, was to say something about that class. Funnily enough, it marked it as something. An example of this is the Serializable interface, that did nothing but marked that a Class was able to be serialized into a String. The question here is:

Does the definition include functionality?

No, I don't think it does. Functionality is more than just metadata about the class; it helps to define the class itself. It takes that step from metadata to data. So in terms of the design pattern, a marker interface can not define or declare functionality; it can simply make a statement about the implementing Class.

like image 26
christopher Avatar answered Sep 28 '22 09:09

christopher


A marker interface can have default methods, but having them is nonsensical.

A marker interface differs from a conventional interface in the way it's used. A conventional interface defines methods, both abstract and default. Thus it is sensible for a program to declare variables with that interface as its type, and to call methods of both kinds through a reference of that interface type.

A marker interface, by constrast, is not used for calling methods. It is a piece of meta-information about an object declared through the type system. It is typically used by calling code via an instanceof expression, or occasionally Class.isAssignableFrom(). It is pointless to declare a variable whose type is a marker interface, since there's nothing you can do with such a variable.

Examples of marker interfaces in the JDK are Cloneable, RandomAccess, and Serializable.

Now consider the addition of a default method to some marker interface:

interface Marker {
    default void foo() { ... }
}

What could the default implementation of foo do?

Implementations of default methods typically want to operate on this, and they do so by calling other instance methods on this. They could call other default methods, but having a bunch of default methods calling each other isn't useful. Eventually, some kind of actual operation on this must be performed. Since interface methods have no access to state (fields), any actual operations must be performed by abstract method implementations residing in an implementing class. However, in a marker interface there are no such methods.

The default implementation of foo could call a static method on this interface or on some other class. This is mostly pointless, as such a method would probably be better expressed as a static method in the first place. The implementation could pass this to a static method, but that method couldn't do anything useful with such a reference, since it has no methods! Well, it might have default methods, but now we're going in circles.

For a default method to be useful on an interface, that interface needs to have abstract methods as well. But if it has abstract methods, it's no longer a marker interface. Thus, it is nonsensical to have default methods on a marker interface.

like image 42
Stuart Marks Avatar answered Sep 28 '22 07:09

Stuart Marks