We all know that marker interface is an empty interface which doesn't have any methods or fields defined in it.Serializable and cloneable are example for the marker interface.
We have studied in various tutorials saying that the marker interface are used to indicate some signal to the Java compiler. Is this the correct definition for marker Intarface?The answer is 'NO'. The correct definition is:
"By Implementing a marker interface we are allowing our class to be carried out a certain operation by an Util class".
For instance if we implement the Serializable Interface,we are allowing our class to be get serialized. In this case the Util class will be ObjectOutputStream which will write our object to the stream. So the method writeObject of ObjectOutputStream will check if the object passed to it has implemented the Serializable interface(By using instanceof).If it does it will continue to serialize the object, otherwise it will throw the NotSerializableException.
The next question comes to our mind, Can we write our own Marker interface? The answer is "YES".
Lets start to write our own marker interface "Objectprinter".By implementing this marker interface,we are saying that our object values are allowed to be printed to a file by an Util class.In our case the util class which will write the object attribute values to a file is "ObjectPrinterUtil".Lets also define two classes
ObjectWriter.java
Person.java
Department.java
ObjectPrinterUtil.java
OUTPUT
As we can see in the output,
We have studied in various tutorials saying that the marker interface are used to indicate some signal to the Java compiler. Is this the correct definition for marker Intarface?The answer is 'NO'. The correct definition is:
"By Implementing a marker interface we are allowing our class to be carried out a certain operation by an Util class".
For instance if we implement the Serializable Interface,we are allowing our class to be get serialized. In this case the Util class will be ObjectOutputStream which will write our object to the stream. So the method writeObject of ObjectOutputStream will check if the object passed to it has implemented the Serializable interface(By using instanceof).If it does it will continue to serialize the object, otherwise it will throw the NotSerializableException.
The next question comes to our mind, Can we write our own Marker interface? The answer is "YES".
Lets start to write our own marker interface "Objectprinter".By implementing this marker interface,we are saying that our object values are allowed to be printed to a file by an Util class.In our case the util class which will write the object attribute values to a file is "ObjectPrinterUtil".Lets also define two classes
- Person which will implement this marker Interface "ObjectPrinter" and
- Department which will not implement it.
ObjectWriter.java
public interface ObjectPrinter { } |
Person.java
public class Person implements ObjectPrinter{
String name;
int age;
public Person(String name,int age)
{
this.name=name;
this.age=age;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
}
|
Department.java
public class Department {
private String departmentId;
private String departmentName;
public Department(String departmentId, String departmentName) {
this.departmentId = departmentId;
this.departmentName = departmentName;
}
@Override
public String toString() {
return "Department [departmentId=" + departmentId + ", departmentName="
+ departmentName + "]";
}
} |
ObjectPrinterUtil.java
public class ObjectPrinterUtil { public static void main(String[] args) throws Exception {
ObjectPrinterUtil objectPrinterUtil=new ObjectPrinterUtil();
Person person= new Person("Ram", 20);
Department department=new Department("D01","CSE");
objectPrinterUtil.printObject(person);
objectPrinterUtil.printObject(department);
}
public void printObject(Object object) throws Exception
{
if(object instanceof ObjectPrinter)
{
System.out.println("Value Printed to the File:"+object.toString());
}
else
{
throw new NonPrintableException();
}
}
}class NonPrintableException extends Exception
{
public NonPrintableException()
{
super("Object is not printable");
}
}
|
- printObject method of the ObjectPrinterUtil is responsible for printing passed object attribute values to the console.
- It will check if the passed object has implemented the ObjectPrinter Interface.If it does then it write the attribute value to the console else it will throw the Exception NonPrintableException.
OUTPUT
Value Printed to the File:Person [name=Ram, age=20]
Exception in thread "main" test.NonPrintableException: Object is not printable
at test.ObjectPrinterUtil.printObject(ObjectPrinterUtil.java:21)
at test.ObjectPrinterUtil.main(ObjectPrinterUtil.java:10)
As we can see in the output,
- Since the Person class implemented the ObjectPrinter marker Interface,its attribute values were printed to the console by the ObjectPrinterUtil.
- Department doesn't implemented this interface,hence the values were not printed and NonPrintable Exception were thrown by the ObjectPrinterUtil.
No comments:
Post a Comment