In which situation Marker Interface will usefull? [duplicate] - java

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.

Related

Why use Java interfaces when you have to redefine methods to override them? [duplicate]

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.

why to use interface if there is only abstract methods [duplicate]

This question already has answers here:
Do/can abstract classes replace interfaces? [duplicate]
(11 answers)
what is the actual use of interface in java? [duplicate]
(6 answers)
Closed 7 years ago.
In java there is interface which having only abstract method means method declration only and those methods are implement in class that implement interface so why to use interface which has only declaration .This question is asked in interview.
Interfaces are used for only declaration of methods because it can achieve multiple behavior. It is the class's responsibility to implement the interfaces and define their methods as they desire.
Interfaces allow same methods to be implemented in different manner by implementing classes, which allows you to achieve behavioral facility.
For understanding sake, you can consider interfaces exist so you can achieve behavioral approach for your application, and class exist to achieve characteristic approach.

How marker interface are identified by JVM? [duplicate]

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.

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.

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