Four important uses of interface with example
Interface is an reference type in Java. There are four important uses of interface
- Use as a collection of utility methods(since java 8)
- Use as constants(variable are defined as constant's)
- Creation of CustomAnnotation's
- Special services(like marker interface)
Use as a collection of utility methods: With java8 release, we can utility(helper) methods in an interface, this helps developer to write a code at one place, inspite to writing it in a different class. The methods which are allowed in an interface are default and static.
import java.util.List;
interface Employee {
public default List<?> getEmpDetails(int empId) {
System.out.println("here we can write our own code...");
return null;}
public static String getEmpName(int empName) {
System.out.println("here code follows....");
return "....";}
}
public class Demo implements Employee {
public static void main(String arg[]) {
Employee emp = new Demo();
System.out.println(emp.getEmpDetails(111));
System.out.println(Employee.getEmpName(111));
}
}
---------------------
Use as constants(variable are defined as constant's) : Variable defined in an interface can be used as constants.
interface WeekDays {
String firstDay = "MONDAY";
String secondDay = "TUESDAY";
String thridDay = "WEDNESDAY";
String fourthDay = "THURSDAY";
String fifthDay = "FRIDAY";
String sixthDay = "SATURDAY";
}
public class Demo {
public static void main(String arg[]) {
System.out.println(WeekDays.firstDay);
System.out.println(WeekDays.sixthDay);
}
}
------------------------------------------------------------------------------------------
Creation of Custom Annotation's : Using interface we can define a custom based annotations like Audit(where we can capture every event of a user, and store it in a database)
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method;
@Retention(RetentionPolicy.RUNTIME)
@interface CustomAnnotation {
String id();
String date();
}
public class Demo {
@CustomAnnotation(id = "123", date = "12/3/2016")
public void getData() {
}
private void showAnnotation() throws NoSuchMethodException, SecurityException {
Demo test = new Demo();
Class c = test.getClass(); // Getting Class reference
Method m = c.getMethod("getData"); // Getting Method reference
CustomAnnotation annotation2 = m.getAnnotation(CustomAnnotation.class);
System.out.println("Author of the method: " + annotation2.id());
System.out.println("Date of Writing the method: " + annotation2.date());
}
public static void main(String[] args) throws NoSuchMethodException, SecurityException {
Demo d = new Demo();
d.getData();
d.showAnnotation();
}
}
------------------------------------------------------------------------------
Special services(like marker interface) Using interface we can create a special services based annotation like maker interface...
interface Marker{
}
public class Demo implements Marker{
public void m1(){
if(this instanceof Marker)
System.out.println("success....you can write your business logic here");
else
System.out.println("Need to implement marker interface....");
}
public static void main(String[] args) {
new Demo().m1();
}
}