This question already has answers here:
How Marker Interface is handled by JVM
(7 answers)
Closed 9 years ago.
Could any body explain me how JVM internally handles marker interfaces like:
Serializable
Just like any other interface... Marker interface is not a language construct, it's just a term used to describe an interface without any methods.
See: http://en.wikipedia.org/wiki/Marker_interface_pattern
The simple answer is it is not different from any other interface. There is absolutely no difference between a marker interface and other interface.
You may check Marker Interface in Java: what, why, uses, etc.
On a side note:-
If we take java.io.Serializable marker interface. Now this doesnot have any members defined in it.
So when a java class is to be serialized, then JVM should be intimiated in some way that if we want we can serialize this java class. So in this case, marker interfaces are used. So the java class which may be serialized has to implement the java.io.Serializable marker interface. Hence now we can initimate the JVM.
Related
This question already has answers here:
Is there more to an interface than having the correct methods
(17 answers)
Closed 1 year ago.
So, you create an interface and define some methods. Then if you implement the interface to a class you must override all of the methods of the interface. Then what is the purpose of an interface in this case, since you are just repeating yourself, rewriting methods?
Java interfaces help you define rules.
For example, if you had a class for animal, you would want every class implementing animal have a method for movement, and number of limbs, the way they emit sounds etc.
You needn't know beforehand what those methods would be, but you do want to establish those set of rules that make an animal, an animal.
To implement Polymorphism concept.
This question already has answers here:
How does Cloneable work in Java and how do I use it?
(6 answers)
Closed 1 year ago.
Attaching the snippet of clone method implementation from object class, although object class does not implement cloneable interface, how is it allowed to use it ?
Another one : Why couldn't clone method could stay in cloneable interface instead of object class ?
What's the significance and importance aka real time application of marker interface which a usual interface could not have provided.
[clone method from object class]
[1]: https://i.stack.imgur.com/QH4Yi.png
although object class does not implement cloneable interface, how is it allowed to use it ?
Search the web for what the cast operator does. It is a Cloneable.
Why couldn't clone method could stay in cloneable interface instead of object class ?
Because interfaces can't declare implementations (since java 1.8 they can via the default mechanism, but Object.clone() predates that by over 20 years or so).
Do not look at the way Cloneable works as an example of good API design. It's not, but the developers of java didn't know any better back when the Cloneable stuff was designed.
This question already has answers here:
How Marker Interface is handled by JVM
(7 answers)
Closed 7 years ago.
I have gone through few question in stack overflow but could not find a suitable answer. So raising it for more clarity.
I know a marker interface is an interface with no methods. When we implement a marker interface for example Serializable it declares that the class implementing it becomes eligible for serialization.
My question is how JVM understands that the objects of class implementing Serializable interface should be serialized. If i write an interface with no methods and hope that objects of class that implements it will be serialized i'll not work that way.
Is it possible for us to create a custom marker class.?
They aren't 'identified by the JVM' at all. They're identified by the Java code that is interested in them, for example ObjectOutputStream, via the instanceof operator.
This question already has answers here:
What is the use of marker interfaces in Java?
(10 answers)
Closed 8 years ago.
What is use of user defined marker interface, and how it works?
In case of already defined marker interfaces such as serializable or cloneable, the JVM do some internal processing, but for the user defined marker interface how JVM behave?
The Marker Interface pattern is a well known pattern that allows you to indicate something about a type without implementing any behaviour. Wikipedia does a better write-up that I can summarize so you should read that.
To answer your question directly, the JVM won't treat your type any different. It will be a type that implements an interface and that is it.
This question already has answers here:
What is the use of marker interfaces in Java?
(10 answers)
What is the purpose of marker interface? [duplicate]
(1 answer)
Closed 8 years ago.
Can any one tell the In which situation Marker Interface will usefull?
1.Marker Interface does not have any methods inside interface.If our class implements any marker interface internally jvm will add some capabilites to our class.
2.Some of the marker interfaces are
Serializable,Cloneable,RandomAccess etc
3.Marker interfaces are introduced to reduce the burden on the developers.Internally jvm takes care of logic when we implement markerinterface to add the capability or functionalty to our class
4.We can write our own marker interface only possible with customization of jvm
Uses
1.Marker interfaces are useful to declare the metadata about the class
2.Marker interfaces are useful in the case other code takes decisions depending on whether an object implements some marker interface.