Java Interface

Java Interface

Another way to achieve abstraction in Java is with interfaces.

An interface is a completely "abstract class" that is used to group related methods with empty bodies.

To access the interface methods, the interface must be "implemented" (kinda like inherited) by another class with the implements keyword (instead of extends). The body of the interface method is provided by the "implement" class.


Why Use Interfaces?

  1. To achieve total abstraction: All methods in an interface are abstract (they have no body).
  2. To achieve multiple inheritance: A class can implement multiple interfaces, while it can only extend one superclass.
  3. To achieve loose coupling: The implementation of a method can be changed without affecting the code that calls it.

Interface Example

Interface Example

// Interface
interface Animal {
  public void animalSound(); // interface method (does not have a body)
  public void sleep(); // interface method (does not have a body)
}

// Pig "implements" the Animal interface class Pig implements Animal { public void animalSound() { // The body of animalSound() is provided here System.out.println("The pig says: wee wee"); } public void sleep() { // The body of sleep() is provided here System.out.println("Zzz"); } }

class Main { public static void main(String[] args) { Pig myPig = new Pig(); myPig.animalSound(); myPig.sleep(); } }

Notes on Interfaces:


Multiple Interfaces

To implement multiple interfaces, separate them with a comma.

Multiple Interfaces Example

interface FirstInterface {
  public void myMethod(); // interface method
}

interface SecondInterface { public void myOtherMethod(); // interface method }

class DemoClass implements FirstInterface, SecondInterface { public void myMethod() { System.out.println("Some text.."); } public void myOtherMethod() { System.out.println("Some other text..."); } }