Types of Interfaces in Java
In Java, interfaces are a powerful feature used to define a set of rules that classes can follow. They help in achieving abstraction and multiple inheritance. Here’s an easy explanation of different types of interfaces in Java, with examples:
1. Marker Interface
A marker interface is an empty interface with no methods or fields. It is used to indicate that a class possesses certain properties.
Example: Serializable
Interface
import java.io.Serializable;
public class MyClass implements Serializable {
private int id;
private String name;
// Constructor, getters, and setters
}
Here, the Serializable
interface is a marker interface indicating that instances of MyClass
can be serialized.
2. Functional Interface
A functional interface is an interface with exactly one abstract method. They are used as the basis for lambda expressions and method references in Java 8 and beyond.
Example: Runnable
Interface
@FunctionalInterface
interface MyFunctionalInterface {
void execute();
}
public class TestFunctionalInterface {
public static void main(String[] args) {
MyFunctionalInterface func = () -> System.out.println("Functional Interface Example");
func.execute();
}
}
In this example, MyFunctionalInterface
is a functional interface with a single abstract method execute
3. Single Inheritance Interface
A single inheritance interface is a standard interface with one or more abstract methods that a class must implement.
Example: Comparable
Interface
public class Student implements Comparable<Student> {
private String name;
private int grade;
public Student(String name, int grade) {
this.name = name;
this.grade = grade;
}
@Override
public int compareTo(Student other) {
return Integer.compare(this.grade, other.grade);
}
// Getters and setters
}
Here, the Student
class implements the Comparable
interface to define an ordering of Student
objects by grade.
4. Multiple Inheritance Interface
Java does not support multiple inheritance for classes, but a class can implement multiple interfaces, achieving multiple inheritance.
Example: Runnable
and Callable
Interfaces
import java.util.concurrent.Callable;
public class Task implements Runnable, Callable<String> {
@Override
public void run() {
System.out.println("Running task...");
}
@Override
public String call() {
return "Task completed";
}
public static void main(String[] args) {
Task task = new Task();
Thread thread = new Thread(task);
thread.start();
try {
System.out.println(task.call());
} catch (Exception e) {
e.printStackTrace();
}
}
}
In this example, the Task
class implements both Runnable
and Callable
interfaces, demonstrating multiple inheritance.
5. Nested Interface
A nested interface is defined inside a class or another interface. It is used for grouping interfaces that are only relevant to the enclosing class or interface.
Example: Nested Interface within a Class
public class OuterClass {
interface NestedInterface {
void nestedMethod();
}
}
class ImplementingClass implements OuterClass.NestedInterface {
@Override
public void nestedMethod() {
System.out.println("Nested Interface Method Implemented");
}
public static void main(String[] args) {
ImplementingClass obj = new ImplementingClass();
obj.nestedMethod();
}
}
Here, NestedInterface
is defined inside OuterClass
, and ImplementingClass
implements it.
6. Tagging Interface
A tagging interface, similar to a marker interface, is used to convey metadata about a class to the JVM or frameworks.
Example: Cloneable
Interface
public class MyCloneableClass implements Cloneable {
private int value;
public MyCloneableClass(int value) {
this.value = value;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
public static void main(String[] args) {
try {
MyCloneableClass obj1 = new MyCloneableClass(10);
MyCloneableClass obj2 = (MyCloneableClass) obj1.clone();
System.out.println("Cloning successful: " + (obj1 != obj2));
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
}
}
In this example, MyCloneableClass
implements the Cloneable
interface, indicating it supports cloning.
These examples show the different types of interfaces in Java and how they are used in different situations.
Thank you for reading! I hope this post helped you understand marker and functional interfaces in Java better. If you found this useful, be sure to check out my other blog posts for more insights and tutorials. Happy coding!