Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
Node is saved as draft in My Content >> Draft
  • Null/ Marker interface

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    Comment on it

    Null Interfaces in Java generally referred as "Marker", are type of interface which have no methods declared in them which means that the classes implementing these interfaces don't have to override any of the methods. "Serializable", "Remote", "RandomAccess " and "Cloneable" are few of the popular marker interfaces in the JDK .

    -> what is the use of implementing these interfaces when we don't have to write any method of them????

    Though Marker interface in Java do not force the programmer to write any specific method as other simple interfaces but these are used for special purpose. A marker used to indicate something to compiler or JVM that the class implementing any of these would have some special behavior. eg-

    If a class is implementing "java.io.Serializable", this is indication to JVM that this class needs special treatment. "java.io.Serializable" interface provides a mechanism of object serialization where an object is represented as a sequence of bytes. When a object of the class implementing "java.io.Serializable" is sent over network , Object serialization writes the object into stream. Once an object is serialized and written into a file, it can be read from the file and deserialized. Deserialization is the process of reading the object from the file and reconstructing the object in the memory. The entire process of serialization and deserialization is JVM independent. An object serialized by one JVM can easily be read across another JVM.

    public class SerializableTest implements java.io.Serializable { public String name; public String address;

    public void SerializableTest () { System.out.println(" checking Serializable " + name + " " + address); } }

    -> can we create custom markers? Yes one can also create interfaces which doesn't have any method i.e. Markers. And these can be used in forcing some kind of functionality without implementing any method.

    interface MyMarker { }

    class CustomMarkerTest implements MyMarker { }

    public class Test { public static void main(String []args){ CustomMarkerTest customMarkerTest = new CustomMarkerTest (); if(customMarkerTest instanceof MyMarker ) { System.out.println("DO when marker"); } else { System.out.println("do something else"); } } }

    Java marker/null interface SerializableTest custom marker Cloneable

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: