Java Interface Tutorial with Rules and Examples

Java Interface Explanation with Infographic Tutorial

Java Interface is nothing but a 100% abstract class and the keyword used to define it is the "interface". Interfaces simply declare all the abstract methods which are to be implemented by the implementing class. Let us know more about interfaces in this Last Minute Java Tutorial.

Abstract methods do not have a body and end with a semicolon after the Right Parentheses ")".

Note: Java-1.8 or Java-8 introduced default methods and static methods which we shall discuss in the next chapter.

Java Interface Rules and Examples

The keyword used to implement an interface is "implements". The concrete class implementing the abstract methods of an interface is not called a Subclass, instead, we simply call it an implementor. So, an interface does not use the keyword "class" in its definition or creation.

Syntax of an Interface:

interface NAME
{
  type VARIABLE1,VARIABLE2; //constants
  abstract return-type METHOD_NAME_1(Parameters);
  abstract return-type METHOD_NAME_2(Parameters);
}

Check the below example on a simple Java Interface. House is an Interface here. DuplexHouse is an implementor class for the interface House. It declares all three abstract methods without any implementation. DuplexHouse implements all these three abstract methods by following the same method signature.

InterfaceExample1.java

interface House
{
  int width=30, length=40; //constants by default
  void addSofa(); //abstract is implicit
  abstract void addDiningTable(); //add abstract if you want
  abstract void addTV();
}

class DuplexHouse implements House
{
  public void addSofa()
  {
    System.out.println("3 Seater Sofa");
  }
  public void addDiningTable()
  {
    System.out.println("6 Seater Dining Table");    
  }
  public void addTV()
  {
    System.out.println("4K CURVED LED TV");    
  }
}
public class InterfaceExample1
{
  public static void main(String[] args)
  {
    //Using interface reference to point Implementor Class object.
    House house = new DuplexHouse();
    house.addSofa();

    DuplexHouse dh = new DuplexHouse();
    dh.addDiningTable();
    dh.addTV();
  }
}
//OUTPUT
3 Seater Sofa
6 Seater Dining Table
4K CURVED LED TV

Java Interface Rules for Creation and Use

Below are all the rules to create and implement an interface successfully in Java.

  1. Java Interfaces can not have constructors. So, the objects of an interface type can not be created with a "new" keyword.
  2. All interface variables are constants by default. So the compiler adds "public" and "final" keywords implicitly before the declaration and definition.
  3. All the methods of an interface are abstract by default. So, even if you do not add an abstract keyword, the compiler adds it for you.
  4. All methods of an Interface are public by default.
  5. An implementor class should add a "public" access modifier explicitly before the method names as part of implementation or definition.
  6. An interface can extend or inherit another interface using the usual keyword "extends". The implementing class, in this case, should implement all the methods of all the interfaces.
  7. An interface can not extend or inherit or subclass a Concrete class.
  8. An abstract class can implement an Interface. In that case, the first concrete subclass of that abstract class should implement all the abstract methods of the interface and abstract class.

Look at another example on the usage of Java interface. Interface Airport has an int variable, flights. TokyoAirport is an implementor of the interface Airport.

InterfaceExample2.java

interface Airport
{
  int flights=30; //public and final
}

class TokyoAirport implements Airport
{
  void show()
  {
    flights++; //error. final variable can not be reassigned
    System.out.println(flights);
  }
}

public class InterfaceExample2
{
  public static void main(String[] args)
  {
    TokyoAirport ta = new TokyoAirport();
    ta.show();
  }
}
//OUTPUT
Error: The final field Airport.flights cannot be assigned

In the above example, the show() method tries to change the value of an interface variable "flights". But it is a constant by default. So, you will get a compiler error.

Implementing Multiple Interfaces in Java

A Java class can implement any number of interfaces. In the class definition, separate interface names after "implements" keyword with Commas(,).

Syntax:

class CLASS_NAME implements interface1, interface2
{
  //implementing methods
}

Look at the example below implementing two interfaces Bird and Animal by a concrete-class Dragon. So, a Dragon has the behaviours of both the Bird and Animal.

InterfaceExample3.java

interface Bird
{
  void fly();
}

interface Animal
{
  void bigTeeth();
}

class Dragon implements Bird, Animal
{
  public void bigTeeth()
  {
    System.out.println("It has big Teeth.");
  }
  public void fly()
  {
    System.out.println("It can fly");
  }
}

public class InterfaceExample3
{
  public static void main(String[] args)
  {
    Dragon dragon = new Dragon();
    dragon.bigTeeth();
    dragon.fly();
  }
}
//OUTPUT
It has big Teeth.
It can fly

Remember that the implementing class should implement abstract methods of all interfaces mentioned.

In the coming chapters, we shall compare an Abstract Class and an Interface with some examples.

Share this Last Minute Java Interface tutorial with your friends and colleagues to encourage authors.

 

 

ExamTray App is now Available on Google Play EXAMTRAY android app link