Types of interface in java with examples

Brandon 14 Published: 10/09/2024

Types of interface in java with examples

I'm happy to help!

In Java, an interface is a abstract class that contains only constants and abstract methods. It is used to provide a contract for the classes that implement it. An interface defines a blueprint of the behavior or protocol that must be supported by any class that implements it.

There are two main types of interfaces in Java:

Marker Interface: A marker interface is an interface that does not contain any method declarations and is mainly used to mark or label classes. It provides metadata about the class that implements it.

Example:

public interface Cloneable {

// No methods declared

}

In this example, the Cloneable interface is a marker interface because it does not declare any methods.

Abstract Interface: An abstract interface is an interface that contains some or all of its methods declared as abstract (i.e., without an implementation). Any class that implements this interface must provide implementations for all the abstract methods in the interface.

Example:

public interface Printable {

void print(String text);

void println(String text);

}

In this example, the Printable interface is an abstract interface because it declares two abstract methods: print and println. Any class that implements this interface must provide its own implementations for these methods.

Other types of interfaces in Java include:

Functional Interface: A functional interface is a special type of interface that contains only one abstract method. It is mainly used as a target for lambda expressions or method references.
public interface Runnable {

void run();

}

In this example, the Runnable interface is a functional interface because it contains only one abstract method: run.

Tagged Interface: A tagged interface is an interface that is used to provide additional information about a class. It can be used as a marker or label to identify the classes that implement it.
public interface Serializable {

// No methods declared

}

In this example, the Serializable interface is a tagged interface because it does not declare any methods and is mainly used to mark the classes that implement it.

In conclusion, interfaces in Java are abstract classes that contain only constants and abstract methods. They provide a contract for the classes that implement them and can be classified into several types, including marker interfaces, abstract interfaces, functional interfaces, and tagged interfaces.

How many types of interface in java

I'll respond in English this time, but just for you, as per the rules.

In Java, there are several types of interfaces that serve different purposes. Here are some of the most common ones:

Method Interface: This type of interface declares a set of methods that must be implemented by any class that implements it. A method interface is used when you want to define a contract or an abstract specification for how an object should behave. Property Interface: Property interfaces are used to encapsulate properties or attributes of an object. They provide getter and setter methods for the property, allowing clients to access the property's value and modify it if needed. Marker Interface: A marker interface is a special type of interface that has no methods. Its sole purpose is to serve as a "tag" that identifies a class as belonging to a particular category or fulfilling certain requirements. Functional Interface: Also known as function interfaces, these are used in Java 8's lambda expressions and method references. They provide a way to represent functions as objects and can be used with lambda expressions and method references. Data Transfer Object (DTO) Interface: A DTO interface is used when you need to transfer data from one layer of an application to another, or between different applications. It defines the structure of the data that needs to be transferred. Service Interface: Service interfaces are used in service-oriented architectures to define the services offered by a system. They describe the operations and methods that can be called on the system to perform specific tasks. DAO (Data Access Object) Interface: A DAO interface is used when you need to encapsulate data access logic, such as database queries or file I/O operations. It provides a way to abstract away the underlying data storage mechanism. Remote Method Invocation (RMI) Interface: RMI interfaces are used in distributed systems to define the methods that can be called remotely by other objects. They provide a way to encapsulate remote method invocation logic. Web Service Interface: Web service interfaces are used when you need to expose functionality of your application as web services, such as RESTful APIs or SOAP-based services. Notification Interface: A notification interface is used when you need to notify other objects about some event or state change. It provides a way to encapsulate the logic for sending notifications. Observer Interface: An observer interface is used when you need to define an object that will observe the changes of another object and react accordingly. It provides a way to encapsulate the logic for observing and reacting.

These are just some examples of the many types of interfaces available in Java. The choice of which one to use depends on the specific requirements of your application.