How marker interface are identified by JVM? [duplicate] - java

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.

Related

Why only java.lang. Object is given superclass in java? [duplicate]

This question already has answers here:
Why Object class is Superclass in java [closed]
(4 answers)
Closed 6 years ago.
Is there any reason sun microsystems make Object for all javaclass for superclass. I face the question my last interview. I hope, I can find answers here
Thanks
Following could be the reasons for this design decision,
By having the Object as the super class of all Java classes, without knowing the type we can pass around objects using the Object declaration.
Before generics was introduced, imagine the state of heterogeneous Java collections. A collection class like ArrayList allows to store any type of classes. It was made possible only by Object class hierarchy.
The other reason would be to bring a common blueprint for all classes and have some list of functions same among them. I am referring to methods likehashCode(), clone(), toString() and methods for threading which is defined in Object class.
Please check the below link. I hope it will answer your question.
Why object is super class in JAVA

example of a class that implements Cloneable Java [duplicate]

This question already has answers here:
How does Cloneable work in Java and how do I use it?
(6 answers)
Closed 6 years ago.
From what I have read about the Cloneable interface its a waste of time but we still have to study it for some reason. I have been going through sample questions but can not find an answer to the one below.
Example of a class that implements clonable?
Anyone have an example of a class that implements cloneable?
---edit---
This question is not a duplicate as this question clearly states that I'm looking for an "Example of a class that implements cloneable?"
The question that ye state it is a duplicate of(stackoverflow.com/questions/4081858/about-java-clone‌​able) does not ask for an example and I had read it and all other cloneable related questions long before posting this question.
Most of the Java Collections Framework classes implement it, so classes like ArrayList, LinkedList, and HashMap.
As Jens points out in the comments, there's a "Use" link at the bottom of the Javadoc that takes you to a page full of uses of the Cloneable interface:
If you scroll down on that page, you'll see long lists of classes in various packages that implement Cloneable.

What is use of user defined marker interface, and how it works? [duplicate]

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.

In which situation Marker Interface will usefull? [duplicate]

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.

How JVM handles marker interfaces internally? [duplicate]

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.

Categories