Marker interface in Java programming.

Java 8Object Oriented ProgrammingProgramming

An empty interface in Java is known as a marker interface i.e. it does not contain any methods or fields by implementing these interfaces a class will exhibit a special behavior with respect to the interface implemented. java.lang.Cloneable and java.io.Serializable are examples of marker interfaces.

Example1

Consider the following example, here we have a class with the name Student which implements the marking interface Cloneable. In the main method we are trying to create an object of the Student class and clone it using the clone() method.

 Live Demo

import java.util.Scanner;
public class Student implements Cloneable {
   int age;
   String name;
   public Student (String name, int age){
      this.age = age;
      this.name = name;
   }
   public void display() {
      System.out.println("Name of the student is: "+name);
      System.out.println("Age of the student is: "+age);
   }
   public static void main (String args[]) throws CloneNotSupportedException {
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter your name: ");
      String name = sc.next();
      System.out.println("Enter your age: ");
      int age = sc.nextInt();
      Student obj = new Student(name, age);
      Student obj2 = (Student) obj.clone();
      obj2.display();
   }
}

Output

Enter your name:
Krishna
Enter your age:
29
Name of the student is: Krishna
Age of the student is: 29

Example2

In the following java program, the class Student has two instance variables name and age where age is declared transient. In another class named ExampleSerialize, we are trying to serialize and desterilize the Student object and display its instance variables. Since the age is made invisible (transient) only the name-value is displayed.

 Live Demo

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
class Student implements Serializable{
   private String name;
   private transient int age;
   public Student(String name, int age){
      this.name = name;
      this.age = age;
   }
   public String getName() {
      return this.name;
   }
   public void setName(String name) {
      this.name = name;
   }
   public void setAge(int age) {
      this.age = age;
   }
   public int getAge() {
      return this.age;
   }
}
public class ExampleSerialize {
   public static void main(String args[]) throws Exception{
      Student std1 = new Student("Krishna", 30);
      FileOutputStream fos = new FileOutputStream("e:\\student.ser");
      ObjectOutputStream oos = new ObjectOutputStream(fos);
      oos.writeObject(std1);
      FileInputStream fis = new FileInputStream("e:\\student.ser");
      ObjectInputStream ois = new ObjectInputStream(fis);
      Student std2 = (Student) ois.readObject();
      System.out.println(std2.getName());
   }
}

Output

Krishna
raja
Published on 06-Sep-2019 12:18:16
Advertisements