Full Stack Web Development Internship Program
- 5k Enrolled Learners
- Weekend/Weekday
- Live Class
This article will brief you on an interesting aspect of Interfaces called as Marker Interface In Java and follow it up with implementation. Following pointers will be covered in this article,
So let us get started then,
Marker interface is an interface which is empty, i.e. it does not contain any methods or fields. It is also known as a tagging interface and is used to indicate or inform the JVM that a class implementing this interface will have some special behaviour. An efficient way to classify code can be achieved using the marker interface. Examples of such an interface are: Serializable, Cloneable and Remote Interface.
Moving with this article on Marker Interface in Java
Serialization in java can be defined as the process of converting the state of an object into a byte stream. This can be achieved by using the serializable interface which is present in java.io.package. It must be noted that the all subtypes of a serializable class are themselves serializable.
Example:
import java.io.*; class Main implements Serializable { int j; String s; // A class constructor public Main(int j,String s) { this.j = j; this.s = s; } } public class Test { public static void main(String[] args) throws IOException, ClassNotFoundException { Main obj = new Main(25,"HelloWorld"); // Serializing 'obj' FileOutputStream fos = new FileOutputStream("pqr.txt"); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(obj); // De-serializing 'obj' FileInputStream fis = new FileInputStream("pqr.txt"); ObjectInputStream ois = new ObjectInputStream(fis); Main b = (Main)ois.readObject(); //down-casting object System.out.println(b.j+" "+b.s); // closing streams oos.close(); ois.close(); } }
Output:
25 HelloWorld
Moving with this article on Marker Interface in Java
This interface can be found in the java.lang package. Cloning is the mechanism of generating a replica or an exact copy of an object with a different name.
The Cloneable Interface is implemented by a class to indicate to the object.clone() method that it is legal for the method to make a field-for-field copy of instances of that class.
A CloneNotSupportedException is thrown for a class which invokes the clone method without implementing a cloneable interface.
Example:
import java.lang.Cloneable; class javaClone implements Cloneable { int j; String s; // Defining a class constructor public javaClone(int j,String s) { this.j = j; this.s = s; } // Overriding clone() method @Override protected Object clone() throws CloneNotSupportedException { return super.clone(); } } public class Main { public static void main(String[] args) throws CloneNotSupportedException { javaClone c = new javaClone(18, "HelloWorld"); // cloning 'c' and holding // new cloned object reference in b // down-casting javaClone b = (javaClone)c.clone(); System.out.println(b.j); System.out.println(b.s); } }
Output:
18
HelloWorld
Moving with this article on Marker Interface in Java
A remote object can be defined as an object whose methods can be invoked from a different JVM, potentially on another host. This interface is found in the java.rmi package. A remote object must implement this method directly or indirectly.
RMI:
Remote Method Convocation is an API that enables an object to invoke methods on an object running in a different JVM. It provides remote communication between the two applications using the following objects: stub and skeleton.
Stub:
A stub can be defined as an object that is present at the client side and represents the remote object. It creates an information block which consists of:
α Identifier of the remote object
α Name of the method which is to be invoked
α Parameters to the remote JVM
Skeleton:
The main job of the skeleton object is to pass the requests from the stub to the remote object. In addition, it performs the tasks given below:
α It invokes the desired method on the original remote object
α Reads the parameter specified for the remote object
Moving with this article on Marker Interface in Java
import java.rmi.*; public interface AddAll extends Remote{ public int add(int r,int s)throws RemoteException; }
Here, the Remote interface is extended, and RemoteException is declared with all the methods of the remote interface.
Moving with this article on Marker Interface in Java
There are two ways to provide implementation to the remote interface:
α Extend the UnicastRemoteObject class
α Use the exportObject() method of the UnicastRemoteObject class
import java.rmi.*; import java.rmi.server.*; public class AddAllRemote extends UnicastRemoteObject implements Adder{ AddAllRemote()throws RemoteException{ super(); } public int add(int r,int s){return r+s;} }
Using rmic (rmi compiler), create the stub and skeleton objects.
The stub and skeleton objects can be created by using the rmi compiler. The rmi tool invokes the RMI compiler to create the objects.
rmic AddAllRemote
Using the rmiregistry tool, start the registry service.
The registry service can be started by using the rmregistry tool. A default port number is used if not specified by the user.
rmiregistry 5000
Moving with this article on Marker Interface in Java
import java.rmi.*; import java.rmi.registry.*; public class Server{ public static void main(String args[]){ try{ AddAll stub=new AddAllRemote(); Naming.rebind("rmi://localhost:5000/sak",stub); }catch(Exception e){System.out.println(e);} } }
The remote object is being bound by the name sak in the above example.
Moving with this article on Marker Interface in Java
In the example given, the server and the client applications are being run on the same machine. Thus, the usage of localhost is being made.
import java.rmi.*; public class Client{ public static void main(String args[]){ try{ AddAll stub=(AddAll)Naming.lookup("rmi://localhost:5000/sak"); System.out.println(stub.add(29,18)); }catch(Exception e){} } }
To access the remote object from a different machine, the local host name must be changed to the IP Address or the host name where the remote object is located.
An efficient way to classify code can be achieved using the marker interface.
Thus we have come to an end of this article. If you wish to learn more, check out the Java Online Training by Edureka, a trusted online learning company. Edureka’s Java J2EE and SOA training and certification course is designed to train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.
Got a question for us? Please mention it in the comments section of this blog and we will get back to you as soon as possible.
Course Name | Date | |
---|---|---|
Java Certification Training Course | Class Starts on 28th January,2023 28th January SAT&SUN (Weekend Batch) | View Details |
Java Certification Training Course | Class Starts on 25th February,2023 25th February SAT&SUN (Weekend Batch) | View Details |
edureka.co