Ad

Wednesday 27 August 2014

What is marker interface and can we write our own marker interface in java?

       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
  •   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. 

Monday 25 August 2014

To disable the contextmenu on rightclick of the link/anchor using jquery

    We sometimes need to disable the right click on the link due to security reasons, so that we don't show up the default context menu on our webpage. Using jQuery this can be easily implemented. Below is the sample page where we have two links(anchor) and we would need to disable the right click on the first link(Link1)

<html>
<head>                                                                                                                                                     
<title>To Disable right click</title>
<script type="text/javascript" src="jquery-2.1.1.min.js" ></script>
<script type="text/javascript">

$(document).ready(function() {

$('#link1').bind("contextmenu",function(e){
alert("Right click not allowed");
return false;
});


});
</script>
</head>
<body bgcolor=white>
<a href="#" id="link1">Link1</a>
<BR>
<a href="#" id="link2">Link2</a>
</body>
.

As we can see in the JavaScript, we registered the handler to the element of id "link1" for the eventType "contextmenu".So this handler will be called when we right click on the link "Link1" and since we return as false the context menu will not be displayed.

Demo
Link1
Link2

If our requirement is to disable the right click on the whole webpage, then we need to register the handler on the document object as shown below:

$(document).bind("contextmenu",function(e){                                                                                      
alert("Right click not allowed");
return false;
});