Java - Interface

Java

← Prev

In this tutorial we will learn about interface in Java programming language.

We learned about abstraction and how to create abstract classes in one of the previous tutorial. Feel free to check that out.

What is interface?

An interface in Java is a class having static constants and abstract methods.

It provides us complete abstraction i.e. interface leaves the implementation part to the classes.

How to create an interface?

We use the interface keyword to create an interface in Java.

Syntax of interface

Following is the syntax of an interface.

interface SomeName {
  retType methName(param_list);
  type varName = val;
}

Where, SomeName is the name of the interface.

methName is the name of the method having return type retType. The list of parameters is denoted by param_list.

varName is the static constant and is assigned val value.

Points to remember

  • We create interface using the interface keyword.
  • The methods inside interface are all abstract methods.
  • The variables inside interface are all static final constants.

Example #1:

In the following example we are creating an interface RandNum.

It has a method getNumber().

And it has two integer constants MAX and MIN.

interface RandNum {

  int getNumber();
  int MAX = 10;
  int MIN = 1;

}

Transform

The interface RandNum we created in Example #1 when compiled will transform into the following.

interface RandNum {

  public abstract int getNumber();
  public static final int MAX = 10;
  public static final int MIN = 1;

}

All methods in an interface are abstract methods.

All variables in an interface are static final constants.

Implementing Interface

We use the implements keyword when a class implements an interface.

Syntax

Following is the syntax for a class implementing an interface.

class ClName implements interface1 {
  // some code goes here...
}

Where, ClName is the name of the class and it is implementing interface interface1.

Class can implement multiple interface

A class can also implement more than one interface. For this we list comma separated interface names.

Following is the syntax of a class implementing multiple interface.

class ClName implements interface1, interface2, interface3 {
  // some code goes here...
}

Where, ClName is the name of the class and it is implementing three interfaces interface1, interface2 and interface3.

Example #2:

In the following example we have a class RandNumGenerator and it implements the RandNum interface.

class RandNumGenerator implements RandNum {
  
  /**
   * This method will return a random integer number.
   *
   * This method is from the interface RandNum.
   * It was abstract and now we are implementing it
   * inside this RandNumGenerator class.
   */
  public int getNumber() {

    /**
     * Using random method of the Math class to
     * get a random floating point number
     * greater than or equal to 0 and
     * less than 1.
     */
    double rand = Math.random();

    /**
     * MAX and MIN are the two static final constants
     * from the interface RandNum.
     *
     * We are using it here in this method to compute
     * an integer random number between MIN and Max
     * both inclusive.
     */
    double randNum = Math.floor((rand * MAX) + MIN);

    /**
     * Returning the random number.
     *
     * Since, the return type of this method is int
     * so, we are type-casting the value to int.
     */
    return (int)randNum;
  }

  /**
   * This will return the value stored in the
   * constant MAX from the RandNum interface.
   */
  public int getMAX() {
    return MAX;
  }

  /**
   * This will return the value stored in the
   * constant MIN from the RandNum interface.
   */
  public int getMIN() {
    return MIN;
  }

}

Partial implementation of an interface

If a class does not fully implements an interface then it must be declared as abstract class.

In the following example we have a class Sample and it does not fully implements the RandNum interface. So, we are declaring it as an abstract class.

abstract class Sample implements RandNum {
  
  public void greetings() {

    System.out.println("Hello from the Sample class...");

    /**
     * Note! the getNumber() method from the
     * interface RandNum is not implemented here
     * in this Sample class so, we have declared
     * this class as abstract.
     */
  }
  
}

Complete Code: RandNum interface

/**
 * RandNum interface
 */
interface RandNum {

  int getNumber();
  int MAX = 10;
  int MIN = 1;

}

/**
 * RandNumGenerator class implementing
 * the RandNum interface.
 */
class RandNumGenerator implements RandNum {
  
  public int getNumber() {
    double rand = Math.random();
    double randNum = Math.floor((rand * MAX) + MIN);
    return (int)randNum;
  }

  public int getMAX() {
    return MAX;
  }

  public int getMIN() {
    return MIN;
  }

}

/**
 * main class
 */
public class Example {
  public static void main(String[] args) {
    // create object
    RandNumGenerator rngObj = new RandNumGenerator();

    // get a random number
    int n = rngObj.getNumber();

    // get max
    int max = rngObj.getMAX();

    // get min
    int min = rngObj.getMIN();

    // output
    System.out.println("Max: " + max);
    System.out.println("Min: " + min);
    System.out.println("Random number: " + n);
  }
}

Output:

Max: 10
Min: 1
Random number: 7
← Prev