Java - Marker Interface

Java - Marker Interface

Interface with no fields or methods i.e. empty interface. Examples of marker interface are Serializable, Cloneable and Remote interface. All these interfaces are empty interfaces.

public interface Cloneable{
//nothing here
}        

Cloneable interface

  • Present in java.lang package
  • There is a clone() method in the Object class and any class that implements Cloneable Interface can use the clone() method to make field-for-field copy of instances of that class.
  • Invoking Object’s clone method on an instance of the class that does not implement the Cloneable interface results in an exception CloneNotSupportedException being thrown. By convention, classes that implement this interface should override Object.clone() method.

package oopsconcept;

public class Car implements Cloneable {
	String carName;
	int mileage;
   // constructor of car class
	public Car(String name, int mil) {
		this.carName = name;
		this.mileage = mil;
	}

	// Overriding clone() method by simply calling Object class clone() method.
	protected Object clone() throws CloneNotSupportedException {
		return super.clone();
	}

	public static void main(String[] args) throws CloneNotSupportedException {
		Car c = new Car("Honda Amaze", 25);
		// cloning 'c' and holding new cloned object reference in d
		// down-casting as clone() return type is Object

		Car d = (Car) c.clone();
		System.out.println(d.carName);
		System.out.println(d.mileage);
	}
}        
Output:
Honda Amaze
25        

Serializable interface :

  • Present in java.io package.
  • It is used to make an object eligible for saving its state into a file which is called Serialization.
  • Classes that do not implement this interface will not have any of their state serialized or deserialized. All subtypes of a serializable class are themselves serializable.

package oopsconcept;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class CarSerializable implements Serializable {
	String carName;
	int mileage;

	// constructor of car class
	public CarSerializable(String name, int mil) {
		this.carName = name;
		this.mileage = mil;
	}

	public static void main(String[] args) throws IOException, ClassNotFoundException {

		CarSerializable c = new CarSerializable("Honda Amaze", 25);

		// Serilaizing 'c'
		File file = new File("person.txt");
		FileOutputStream fo = new FileOutputStream(file);

		ObjectOutputStream ob = new ObjectOutputStream(fo);
		ob.writeObject(c);
		System.out.println("File path: " + file.getAbsolutePath());
		// Deserializing 'c'
		FileInputStream fi = new FileInputStream("file.txt");
		ObjectInput ob1 = new ObjectInputStream(fi);
		// down-casting object
		CarSerializable d = (CarSerializable) ob1.readObject();

		System.out.println(d.carName + ":" + d.mileage);

		// Close the streams
		ob.close();
		ob1.close();

	}}        
Output:
File path: D:\Selenium\Java Interview Question\person.txt
Honda Amaze:25        

Remote interface :

  • Present in java.rmi package
  • A remote object is an object which is stored at one machine and accessed from another machine. So, to make an object a remote object, we need to flag it with Remote interface. Here, Remote interface serves to identify interfaces whose methods may be invoked from a non-local virtual machine.Any object that is a remote object must directly or indirectly implement this interface.
  • RMI (Remote Method Invocation) provides some convenience classes that remote object implementations can extend which facilitate remote object creation.

要查看或添加评论,请登录

Karishma Yadav的更多文章

  • JAVA Singleton Design Pattern

    JAVA Singleton Design Pattern

    Introduction Ensuring that only one class instance exists can be crucial in software development. The Singleton Pattern…

  • JAVA - String Vs StringBuilder Vs StringBuffer

    JAVA - String Vs StringBuilder Vs StringBuffer

    String It is a sequence of characters. String Class objects are immutable, meaning they cannot be changed once created.

  • Selenium 4- Launch Chrome, Firefox and Edge Browser

    Selenium 4- Launch Chrome, Firefox and Edge Browser

    Pre-requisite to run the selenium test script IDE - Eclipse IDE for Java Developers (version- 4.26.

  • Fundamentals of Git and GitHub

    Fundamentals of Git and GitHub

    What is Git? It is a Version Control System that helps us to track code changes. Why Git? Free and Open Source Fast And…

  • Assertions in Taiko

    Assertions in Taiko

    Assertions is a mechanism used to compare the actual output with the expected output while testing the functionality…

    1 条评论
  • Javascript - Miscellaneous

    Javascript - Miscellaneous

    Difference between null and undefined null and undefined are two distinct types that represent different values. By…

    3 条评论
  • Functions in Javascript

    Functions in Javascript

    A JavaScript function is a block of code designed to perform a particular task. Function Definition - Before we use a…

  • Higher-order Functions in JS

    Higher-order Functions in JS

    Higher order functions are functions that take one or more functions as arguments, or returns a function as its result.…

  • Arrow Functions In Javascript

    Arrow Functions In Javascript

    Arrow functions were introduced in the ES6 version. They make the code more structured and readable.

  • Looping Statements

    Looping Statements

    Looping statement is fundamental control structures in programming. Looping statements are used to execute a block of…

社区洞察

其他会员也浏览了