Java Interface


Interface is a pure abstract class. They are syntactically similar to classes, but you cannot create instance of an Interface and their methods are declared without any body.

Interface is used to achieve complete abstraction in Java. When you create an interface it defines what a class can do without saying anything about how the class will do it.

Syntax

interface interface_name { }

Example of Interface

interface emp
{ 
 int emp_id=1212;
 void id_show();
}

NOTE: Compiler automatically converts methods of Interface as public and abstract, and the data members as public, static and final by default.


Rules for using Interface

  • Methods inside Interface must not be static, final, native or strictfp.
  • All variables declared inside interface are implicitly public static final variables(constants).
  • All methods declared inside Java Interfaces are implicitly public and abstract, even if you don't use public or abstract keyword.
  • Interface can extend one or more other interface.
  • Interface cannot implement a class.
  • Interface can be nested inside another interface.

Example of Interface implementation

interface jk201
{
    int roll=30;
    void display();
}
class section implements jk201
{
    public void display()
    {
        System.out.println("RollNumber is ="+roll);
    }
}
public class NewMain {

    public static void main(String[] args) {
        section object = new section(); // create object of class section 
        object.display();
    }
}
Output
RollNumber is =30

Interfaces supports Multiple Inheritance

Though classes in java doesn't suppost multiple inheritance, but a class can implement more than one interface.

interface emp_id
{
    int id =1212;
    void id_show();
}

interface emp_phone
{
    long phone=123456789;
    void phone_show();
}

class show_details  implements emp_id , emp_phone
{
    public void id_show()
    {
     System.out.println("Employee id = "+id);
    }
    public void phone_show()
    {
        System.out.println("Employee Number = "+phone);
    }
}

public class nm2 {


    public static void main(String[] args) {
        show_details  object = new show_details ();
        object.id_show();
        object.phone_show();
    }
}
Output
Employee id = 1212
Employee Number = 123456789

Interface extends other Interface

Classes implements interfaces, but an interface extends other interface.

interface NewsPaper
{
 news();
}

interface Magazine extends NewsPaper
{
 colorful();
}


Difference between an interface and an abstract class?

Abstract class
Interface
Abstract class is a class which contain one or more abstract methods, which has to be implemented by its sub classes. Interface is a Java Object containing method declaration but no implementation. The classes which implement the Interfaces must provide the method definition for all the methods.
Abstract class is a Class prefix with an abstract keyword followed by Class definition. Interface is a pure abstract class which starts with interface keyword.
Abstract class can also contain concrete methods. Whereas, Interface contains all abstract methods and final variable declarations.
Abstract classes are useful in a situation that Some general methods should be implemented and specialization behavior should be implemented by child classes. Interfaces are useful in a situation that all properties should be implemented.

Share this article on