Open In App

Types of Interfaces in Java

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:



  1. An interface can contain any number of methods in that interface.
  2. An Interface can have constant values, which are implicitly defined as public static final.
  3. 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.
  4. The byte code of a given interface will be created in a –  .class file.
  5. 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.






public interface NameOfTheinterface
{
 
  // Any final, static fields here
 
  // Any abstract method declaration here
 
}
//This is Declaration of the interface

ONE SIMPLE EXAMPLE TO UNDERSTAND INTERFACE: 




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();
    }
}

Output
avi
23

Properties of an Interface

An Interfaces have the following properties:

Example: Filename – Car.java




// This is Program To implement the Interface
interface car
{
   void display();
}
 
class model implements car
{
   public void display()
   {
    
       System.out.println("im a Car");
                     // the code output will print "im a car"
   }
 
  public static void main(String args[])
  
    {
   model obj = new model();
   obj.display();
    }
}

Output
im a Car

Types of Interfaces

  1. Functional Interface
  2. Marker interface

 1. Functional Interface:

When an interface contains only one abstract method, then it is known as a Functional Interface.

Examples of Functional Interfaces:

Now we will see an example of a Functional Interface – 

Example:




// This is Program To implement the Functional Interface
 
interface Writable
    void write(String txt); 
        // FuninterExp is a Example of Functional Interface
   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:

For Example:




// Simple Example to understand marker interface
 
public interface interface_name 
   
    
         // empty
    }

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

  1. Cloneable Interface
  2. Serializable Interface
  3. Remote Interface

1. Cloneable Interface

Note – A class that implements the Cloneable interface must override the clone() method by using a public method.

For Example:




// This is Program To implement the Cloneable Interface
 import java.lang.Cloneable;
 
 
 class abc implements Cloneable
// Here The abc is a class constructor
{
    int x;
    String y;
    // Here The abc is a class constructor
    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:

Serialization – Converting an object into byte stream.

Deserialization – Converting byte stream into an object.

3. Remote Interface:


Article Tags :