Java Interface

Suman Barik
0

 

Interfaces

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:


interface Animal {

  public void animalSound(); // interface method (does not have a body)

  public void sleep(); // interface method (does not have a body)

}


class Pig implements Animal {

  public void animalSound() {

    System.out.println("The pig says: wee wee");

  }

  public void sleep() {

    System.out.println("Zzz");

  }

}


class Main {

  public static void main(String[] args) {

    Pig myPig = new Pig();

    myPig.animalSound();

    myPig.sleep();

  }

}


Post a Comment

0Comments
Post a Comment (0)