Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Marker/Null interface in java

    • 0
    • 12
    • 0
    • 0
    • 2
    • 0
    • 0
    • 0
    • 296
    Comment on it

    Marker Interfaces or Null interface in Java are special type of interfaces in character as they have no methods declared in them which means that if any classes implementing these interfaces don't have to override any of the methods. JDK provides a number of marker in Java like Serializable ,Cloneable, Remote , ThreadSafe interface etc

    In addition marker interface can be described as a design pattern. This is used by many languages to provide run-time type information about the objects. The marker interface provides a way to associate metadata with the class where the language support is not available.

    How marker interfaces are useful in Java as they don't have any methods to implement???? Yes marker interfaces don't force programmer to write any methods while implementing them but they have a lot other significance. Marker interface in Java e.g. Serializable, Clonnable and Remote is used to indicate something to compiler or JVM, that the class which is implementing any of these would have to be treated with some special way and that class will show some special behavior. e.g. if a class implements Serializable interface it is indication to the JVM that the object of that class can be represented as a sequence of bytes that includes the object's data as well as information about the object's type and the types of data stored in the object. After a serialized object has been written into a file, it can be read from the file and deserialized that is, the type information and bytes that represent the object and its data can be used to recreate the object in memory. Most impressive is that the entire process is JVM independent, meaning an object can be serialized on one platform and deserialized on an entirely different platform.

    import java.io.*;
    
    public class SerializeDemo
    {
       public static void main(String [] args)
       {
          Student student = new Student ();
          student.name = "Surya";
          student.address = "Dehradun";
          student.id= 101 
       try
          {
             FileOutputStream fileOutputStream = new FileOutputStream("/tmp/students.text");
             ObjectOutputStream out = new ObjectOutputStream(fileOut);
             out.writeObject(student );
             out.close();
             fileOut.close();
    
             System.out.printf("data is serialized and saved to /tmp/students.text ");
          }
    
       catch(IOException exception)
          {
    
          }
       }
    }
    

    can we create custom markers?????

    Yes, apart from using the built-in marker interface, to mark a class as serializable or clonnable, we can also have our own marker interface. The purpose of these interfaces is to force some kind of functionality in the classes by providing some functionality to a class if it implements the marker interface.

    public interface MyCustomMarker
    {
    
    }
    
    public class MarkerImpl implements MyCustomMarker
    {
    }
    
    public class MarkerTest
    {
    
    public static void main(String args[])
    {
      MarkerImpl markerImpl = new MarkerImpl ();
    
    
    if (markerImpl  instanceof MyCustomMarker) 
    {
        System.out.println("do this");
    }
    else
    {
    System.out.println("do other");
    }
    

 2 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: