Reputation: 89
I have read about marker interfaces and I have developed some understanding as to how they can be used in code but what I don't understand why do we need them and what is the reason that this concept was introduced.
I search on the internet and all the answer are along the lines of "they provide some essential information to the JVM so that JVM may perform some useful operations" but the question is that why JVM needs to know that and what possible operation does the JVM has to perform? Why can't I just clone the object without extending the class from Clonable? Why cant I serialize the class object without extending it from Serializable?
Marker interfaces doesn't make much sense and is clearly not a good design in my opinion. An interface WITHOUT any methods??? What's the point?
Now apparently an annotation is here to be used instead of marker interface but still the question is that why JVM needs to know?
Upvotes: 5
Views: 5587
Reputation: 719739
You clearly understand what marker interfaces do. You said so yourself!
So why do we still need them?
In one sense, we don't need them. Anything you can express with marker interfaces you can express using annotations. There is little doubt that annotations are a better way1 to solve the problem.
The fundamental reason we (still) have marker interfaces is History:
Java annotations were added to the Java language in Java 5.0. Prior to that, marker interfaces were the best solution available.
Removing marker interfaces from a class breaks binary compatibility.
There are a small number of Java SE marker interfaces that predate Java 5.0, and are extensively used in user code. The two that spring to mind are Serializable
and Cloneable
.
Whether we like it or not, Java has marker interfaces and that is unlikely to change.
1 - For example, annotations can be declared as not inherited (the default) or inherited.
Marker interfaces doesn't make much sense and is clearly not a good design in my opinion.
Well yes. From the perspective of 2019, that is true.
From the perspective of 1997, when Java was new and C# was 5 years away, the only mainstream(-ish) languages with anything like annotations were versions of LISP.
Now I am pretty sure that the designers of the Java class library back in the Java 1.0 era were aware that marker interfaces were a flawed idea. But they had no alternatives. It was use marker interfaces, or put off the design and implementation of some pretty Java fundamental APIs for 5 or more years.
In the world of enterprise computing, you need to make compromises to get the job done.
Upvotes: 8
Reputation: 960
I can answer your partial question - What is the point of marker interface in java?
By introducing annotations, Java has provided us with an alternative to achieve the same results as the marker interfaces. Moreover, like marker interfaces, we can apply annotations to any class, and we can use them as indicators to perform certain actions.
So what is the key difference?
Unlike annotations, interfaces allow us to take advantage of polymorphism. As a result, we can add additional restrictions to the marker interface.
For instance, let's add a restriction that only a Shape type can be removed from the database:
public interface Shape {
double getArea();
double getCircumference();
}
In this case, our marker interface, let's call it DeletableShape, will look like the following:
public interface DeletableShape extends Shape {
}
Then our class will implement the marker interface:
public class Rectangle implements DeletableShape {
// implementation details
}
Therefore, all DeletableShape implementations are also Shape implementations. Obviously, we can't do that using annotations.
However, every design decision has trade-offs and polymorphism can be used as a counter-argument against marker interfaces. In our example, every class extending Rectangle will automatically implement DeletableShape.
This might not provide exact answer what you want. But could partially help you.
Source: baeldung.com
Upvotes: 0
Reputation: 58902
The points any class can implement it without adding any additional methods/fields/overhead
You just mark class and it's ready to be used/serialized
Even after Java 5.0 where annotation introduced,
There are still also marker annotations as @Configuration
@Configuration annotation is used for Spring annotation based configuration. The @Configuration is a marker annotation which indicates that a class declares one or more @Bean methods and may be processed by the Spring container to generate bean definitions and service requests for those beans at runtime
Upvotes: 0
Reputation: 12513
You don't need to implement Serializable to have serialization, or Cloneable to have cloning. The point of those interfaces is that Java has built in mechanisms for cloning and serialization, and you only need to implement those interfaces if you wish to make use of those inbuilt mechanisms.
So just by implementing Serializable, you are automatically able to use that class with ObjectInputStream and ObjectOutputStream without any extra code.
Cloneable, on the other hand, I find virtually useless. Normally you just implement Cloneable and override Object.clone
and make it public, and you instantly have cloning. But it is said to be broken and you are better off just coding your own cloning logic, like with a copy constructor.
Upvotes: 1