Sponsored Links
Ad by Google
This types of questions mostly asked for fresher level or 2-3 years of experienced candidates only.
Marker interface in java is an empty interface and used to define type of the class. Serializable is a marker interface whenever a class implements a marker interface that class got a special treatment via special methods of java library.
For example, If a class implements Serializable interface and going to be serialized then writeObject(obj) method of ObjectOutputStream class does the validation check for the object with the help of instanceof of operator. If object is an instanceof Serializable then write as ordinary object otherwise throw NotSerializableException exception. There is no magic inside the JVM for the Serializable it's all about the writeObject(obj) method who take cares of Serializable object.
Here is how serializable interface used inside writeObject(obj) method:
if (obj instanceof Serializable) {
writeOrdinaryObject(obj, desc, unshared);
}
In a nutshell, Marker interface as its name suggest it is used to marks a class as being of a specific type. Once you mark a class for a specific type, then write codes to check for the existence of the marker and do something based on that information.
For example here is a MarkWinter Marker interface, if a class implements MarkWinter interface that class got a special discount see in Product.java class
MarkWinter.java
Product.java
Marker interface in java is available since JDK 1, from JDK 5 onwards annotation is used for the same purpose.
Note: Annotations are more powerful then Marker interface. To mark a class for a specific type use annotations, It was not available in JDK 1 but from 1.5 onwards it's.
That's it.
Marker interface in java is an empty interface and used to define type of the class. Serializable is a marker interface whenever a class implements a marker interface that class got a special treatment via special methods of java library.
For example, If a class implements Serializable interface and going to be serialized then writeObject(obj) method of ObjectOutputStream class does the validation check for the object with the help of instanceof of operator. If object is an instanceof Serializable then write as ordinary object otherwise throw NotSerializableException exception. There is no magic inside the JVM for the Serializable it's all about the writeObject(obj) method who take cares of Serializable object.
Here is how serializable interface used inside writeObject(obj) method:
if (obj instanceof Serializable) {
writeOrdinaryObject(obj, desc, unshared);
}
In a nutshell, Marker interface as its name suggest it is used to marks a class as being of a specific type. Once you mark a class for a specific type, then write codes to check for the existence of the marker and do something based on that information.
For example here is a MarkWinter Marker interface, if a class implements MarkWinter interface that class got a special discount see in Product.java class
MarkWinter.java
package com.test; /** * MarkWinter Marker interface,Classes that do not implement this * interface will not get any discount on any product. */ public interface MarkWinter { }Here is a Product class to implements MarkWinter interface and got a special treatment with 30% discount while placing order of winter product.
Product.java
package com.test; public class Product implements MarkWinter { private double price = 50; private double applyDiscount(){ return 30; } public double calculateOrder(Product product){ if(product instanceof MarkWinter){ return price/100*applyDiscount(); } return price; } public static void main(String[] args) { System.out.println("Total product price = "+new Product(). calculateOrder(new Product())); } }
Marker interface in java is available since JDK 1, from JDK 5 onwards annotation is used for the same purpose.
Note: Annotations are more powerful then Marker interface. To mark a class for a specific type use annotations, It was not available in JDK 1 but from 1.5 onwards it's.
Here are few Java Marker interfaces
- java.io.Serializable
- java.lang.Cloneable
- java.util.EventListener
- java.rmi.Remote
That's it.
Sponsored Links
marker interface in java is a kind of interface which has no method is known as marker interface. Serializable, Clonnable is the example of marker interface
ReplyDelete