Types of Interfaces in Java
Last Updated :
14 Mar, 2023
In Java, an interface is a reference type similar to a class that can contain only constants, the method signatures, default methods, and static methods, and its Nested types. In interfaces, method bodies exist only for default methods and static methods. Writing an interface is similar to writing to a standard class. Still, a class describes the attributes and internal behaviors objects, and an interface contains behaviors that a class implements. On the other side unless the class that implements the interface is purely abstract and all the interface methods need to be defined in that given usable class.
An interface is Similar To a Class In Following Ways:
- An interface can contain any number of methods in that interface.
- An Interface can have constant values, which are implicitly defined as public static final.
- An interface name is written in a file with a – (.java extension ) with the name of the interface must be matching the name of the file of that Java program.
- The byte code of a given interface will be created in a – .class file.
- Interfaces appear in packages, and their corresponding bytecode file must similarly be in a structure that matches the package name with it.
How To Declare Interfaces?
The interface keyword is used to declare an interface. Here We have a simple example of declaring an interface.
Java
public interface NameOfTheinterface
{
}
|
ONE SIMPLE EXAMPLE TO UNDERSTAND INTERFACE:
Java
interface GFG
{
void printInfo();
}
class avi implements GFG
{
public void printInfo()
{
String name= "avi" ;
int age= 23 ;
System.out.println(name);
System.out.println(age);
}
}
class interfacesss
{
public static void main (String args[])
{
avi s = new avi();
s.printInfo();
}
}
|
Properties of an Interface
An Interfaces have the following properties:
- An interface is implicitly pure abstract.
- No need to use the abstract keyword while declaring an interface
- Each method in an interface is also implicitly abstract, so the abstract keyword is not needed
- The Methods in an interface are implicitly public within it
Example: Filename – Car.java
Java
interface car
{
void display();
}
class model implements car
{
public void display()
{
System.out.println( "im a Car" );
}
public static void main(String args[])
{
model obj = new model();
obj.display();
}
}
|
Types of Interfaces
- Functional Interface
- Marker interface
1. Functional Interface:
- Functional Interface is an interface that has only pure one abstract method.
- It can have any number of static and default methods and also even public methods of java.lang.Object classes
When an interface contains only one abstract method, then it is known as a Functional Interface.
Examples of Functional Interfaces:
- Runnable : It contains only run() method
- ActionListener : It contains only actionPerformed()
- ItemListener : It contains only itemStateChanged() method
Now we will see an example of a Functional Interface –
Example:
Java
interface Writable
{
void write(String txt);
}
public class FuninterExp implements Writable
{
public void write(String txt)
{
System.out.println(txt);
}
public static void main(String[] args)
{
FuninterExp obj = new FuninterExp();
obj.write( " GFG - GEEKS FOR GEEKS " );
}
}
|
Output
GFG - GEEKS FOR GEEKS
2. Marker Interface:
- An interface that does not contain any methods, fields, Abstract Methods, and any Constants is Called a Marker interface.
- Also, if an interface is empty, then it is known as Marker Interface.
- The Serializable and the Cloneable interfaces are examples of Marker interfaces.
For Example:
Java
public interface interface_name
{
}
|
There are two alternatives to the marker interface that produce the same result as the marker interface.
1) Internal Flags – It is used in the place of the Marker interface to implement any specific operation.
2) Annotations – By applying annotations to any class, we can perform specific actions on it.
Built-in Marker Interface
There are three types of Built-In Marker Interfaces in Java. These are
- Cloneable Interface
- Serializable Interface
- Remote Interface
1. Cloneable Interface
- A cloneable interface in Java is also a Marker interface that belongs to java.lang packages.
- It generates a replica(copy) of an object with a different name. Therefore we can implement the interface in the class of which class object is to be cloned.
- It implements the clone() method of the Object class to it.
Note – A class that implements the Cloneable interface must override the clone() method by using a public method.
For Example:
Java
import java.lang.Cloneable;
class abc implements Cloneable
{
int x;
String y;
public abc( int x,String y)
{
this .x = x;
this .y = y;
}
protected Object clone()
throws CloneNotSupportedException
{
return super .clone();
}
}
public class Test
{
public static void main(String[] args)
throws CloneNotSupportedException
{
abc p = new abc( 10 , "We Are Reading GFG Now" );
abc q = (abc)p.clone();
System.out.println(q.x);
System.out.println(q.y);
}
}
|
Output
10
We Are Reading GFG Now
2. Serializable Interface:
- It is a marker interface in Java that is defined in the java.io package. If we want to make the class serializable, we must implement the Serializable interface. If a class implements the Serializable interface, we can serialize or deserialize the state of an object of that class.
- Serialization is a mechanism in which our object state is ready from memory and written into a file or from the databases.
- Deserialization- is the opposite of serialization means that object state reading from a file or database and written back into memory is called deserialization of an object.
Serialization – Converting an object into byte stream.
Deserialization – Converting byte stream into an object.
3. Remote Interface:
- A remote interface is a marker interface that belongs to java.rmi package. It marks an object as a remote that can be accessed from the host of another machine.
- We need to implement the Remote interface if we want to make an object remote then. Therefore, It identifies the interface.
- A remote interface serves to identify interfaces whose methods may be invoked from a non-local virtual machine. Any object that is a remote object must directly or indirectly implement this interface.
- The remote interface is an interface that declares the set of methods that will be invoked from a remote Java Virtual Machine, i.e.(JVM
Similar Reads
Map Interface in Java
In Java, the Map Interface is part of the java.util package and represents a mapping between a key and a value. The Java Map interface is not a subtype of the Collections interface. So, it behaves differently from the rest of the collection types. Key Features: No Duplicates in Keys: Keys should be
12 min read
Nested Interface in Java
We can declare interfaces as members of a class or another interface. Such an interface is called a member interface or nested interface. Interfaces declared outside any class can have only public and default (package-private) access specifiers. In Java, nested interfaces (interfaces declared inside
5 min read
Queue Interface In Java
The Queue Interface is a part of java.util package and extends the Collection interface. It stores and processes the data in order means elements are inserted at the end and removed from the front. Key Features: Most implementations, like PriorityQueue, do not allow null elements.Implementation Clas
12 min read
Marker Interface in Java
Marker Interface in Java is an empty interface means having no field or methods. Examples of marker interface are Serializable, Cloneable and Remote interface. All these interfaces are empty interfaces. Example: [GFGTABS] Java //Driver Code Starts{ interface Serializable { // Marker Interface } //Dr
4 min read
Interfaces and Polymorphism in Java
Java language is one of the most popular languages among all programming languages. There are several advantages of using the java programming language, whether for security purposes or building large distribution projects. One of the advantages of using JA is that Java tries to connect every concep
6 min read
Private Methods in Java 9 Interfaces
Java 9 onwards, you can include private methods in interfaces. Before Java 9 it was not possible. Interfaces till Java 7 In Java SE 7 or earlier versions, an interface can have only two things i.e. Constant variables and Abstract methods. These interface methods MUST be implemented by classes which
4 min read
UnaryOperator Interface in Java
The UnaryOperator Interface<T> is a part of the java.util.function package which has been introduced since Java 8, to implement functional programming in Java. It represents a function which takes in one argument and operates on it. However what distinguishes it from a normal Function is that
2 min read
Which Java Types Can Implement Interfaces?
In Java there is no concept of multiple-inheritance, but with the help of interface we can achieve multiple-inheritance. An interface is a named collection of definition. (without implementation) An interface in Java is a special kind of class. Like classes, interface contains methods and members; u
7 min read
Evolution of interface in Java
In Java, interfaces define a contract for classes without specifying how the methods are implemented. With time, Java has introduced major enhancements or we can say evolution of interfaces to make them more powerful. So, in this article, we will explore the evolution of interfaces in Java from Java
3 min read
Java List Interface
The List Interface in Java extends the Collection Interface and is a part of the java.util package. It is used to store the ordered collections of elements. In a Java List, we can organize and manage the data sequentially. Key Features: Maintained the order of elements in which they are added.Allows
15+ min read