Java – Object Oriented Programming Concepts – Interface


What is an Interface in java ?

An interface in java is a blueprint of a class.

It is mainly used to achieve abstraction and multiple inheritance in Java.

Interfaces cannot be instantiated, but it can be implemented by “implements” keyword.

Why we use Interface in java ?

1. To achieve abstraction.

2. For supporting the functionality of Multiple Inheritance.

What are the advantages of Interface ?

1. we can achieve the safe-secure flow of implementation.

2. we can achieve Multiple-Inheritance in using Interfaces ( while java doesn’t support Multiple-Inheritance ).

Important features of Interface :

1. We can’t create instance of interface but we can make reference of it that refers to the Object of its implementing class.

2. Single or Multiple interfaces can be implemented by the class.

3. Interfaces can extends another interface or interfaces (multiple interface is possible) .

4. Class which implementing interface must implements all the methods in interface.


5. All the functions/methods are public and abstract. 

6. And all the fields/variables are public, static, and final.

7. Interfaces are used to achieve multiple inheritance.

Syntax :

interface (interface-name){  
}  

Example 1 :

public class Eg1 implements interfaceNew{
public static void main(String args[])
{
String eg = "Interface Example";
Eg1 obj = new Eg1();
System.out.println(eg);
System.out.println("Printing interface value a ="+obj.a);
}
}
interface interfaceNew{
int a = 100;
}

Output :

Interface Example

Printing interface value a =100

1. Interface as Inheritance

An interface can not implement another interface. It can extend the other interface.

Example 1 :

public class Eg2 implements interface1{
public static void main(String args[])
{
String eg = "Printing Interface variable";
System.out.println(eg);
Eg2 obj = new Eg2();
System.out.println("Printing the Interface1 String value name "+obj.name);
System.out.println("Printing the Interface2 String value name "+obj.name);
}
}
interface interface1 extends interface2{
String name = "Interface1";
}
interface interface2{
String name = "Interface2";
}

Output :

Printing Interface variable

Printing the Interface1 String value name Interface1

Printing the Interface2 String value name Interface1

2. Interfaces with Methods in JDK8

We can create method in interfaces and methods can be called by creating objects.

Example 1 :

interface interface1
{
    default void printInfo()
    {
        System.out.println("Printing inside the method declared in interface1");
    }
}
class Eg3 implements interface1
{
    public static void main (String[] args)
    {
        Eg3 obj = new Eg3();
        obj.printInfo();
    }
}

Output :

Printing inside the method declared in interface1.

3. Interfaces with Static Methods in JDK8

In case of static methods defined inside interfaces can be called independently without an object.

Example 1 :

interface interface1
{
    static void printInfo()
    {
        System.out.println("Printing static methods inside interface1 without creating objects");
    }
}
class Eg4 implements interface1
{
    public static void main (String[] args)
    {
        interface1.printInfo();
    }
}

Output :

Printing static methods inside interface1 without creating objects

JDK 9 Interface Features.

Interface can contain


1.Static methods

2.Private methods

3.Private Static methods

Nested Interface :

An interface with another interface declared inside is known as nested interface.

Syntax :

interface interface1{  
 interface interface2{  
 }  
}   

Example 1 :

class Eg5 implements interface1.interface2{
 public void method1()
 {
         System.out.println("Printing nested interface");
 }
 public static void main(String args[]){
  interface1.interface2 obj = new Eg5();
  obj.method1();
 }
}
interface interface1{
  void method2();
  interface interface2{
   void method1();
  }
}

Output :

Printing nested interface