Four important uses of interface with example

Four important uses of interface with example

Interface is an reference type in Java. There are four important uses of interface

  1. Use as a collection of utility methods(since java 8)
  2. Use as constants(variable are defined as constant's)
  3. Creation of CustomAnnotation's
  4. 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();
}
}

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

Muhammed Younus Attari的更多文章

  • Program to delete an element from an array + shrinking size of an array?

    Program to delete an element from an array + shrinking size of an array?

    Enter the values to be removed from an array-->validates if the values exist or not-->if exist then remove and shrink…

  • Custom ArrayList

    Custom ArrayList

    ArrayList is defined in java.util package, it is widely used class in Collection framework.

  • Top Java Projects

    Top Java Projects

    I have few important projects which helps both freshers and experienced java resources. New Patient Registry Management…

社区洞察

其他会员也浏览了