Curriculum
Course: Java Basic
Login

Curriculum

Java Basic

Java Home

0/1

Java Introduction

0/1

Java Get Started

0/1

Java Syntax

0/1

Java Comments

0/1

Java Type Casting

0/1

Java Operators

0/1

Java Booleans

0/1

Java Switch

0/1

Java Break / Continue

0/1

Java Errors and Exception

0/1
Text lesson

implements

Example

An interface serves as an abstract “class” utilized for organizing related methods, each lacking implementation details.

To utilize the methods defined in an interface, another class must “implement” it using the “implements” keyword (instead of “extends”). The implementing class furnishes the implementation details for the interface methods.

// 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
lass 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
MyMainClass {
  public static void main(String[] args) {
    Pig myPig = new Pig();  // Create a Pig object
    myPig.animalSound();
    myPig.sleep();
 }
}

Definition and Usage

The “implements” keyword is employed to enact an interface.

The “interface” keyword is utilized to declare a distinct class type that exclusively comprises abstract methods.

To utilize the methods defined in an interface, another class must “implement” it using the “implements” keyword (similar to inheritance), rather than “extends.” The implementing class supplies the implementation details for the interface methods.

Notes on Interfaces:

  • Objects cannot be instantiated directly from an interface; for instance, in the provided example, creating an “Animal” object within MyMainClass is not feasible.
  • Interface methods lack implementation; their implementation is provided by the implementing class.
  • When implementing an interface, all its methods must be overridden.
  • Interface methods are inherently abstract and public.
  • Interface attributes are inherently public, static, and final.
  • An interface cannot include a constructor, as it doesn’t facilitate object creation.

Why And When To Use Interfaces?

To ensure security by concealing specific details and revealing only the essential aspects of an object (interface), Java employs interfaces.

Java does not facilitate “multiple inheritance” (where a class inherits from more than one superclass). Nevertheless, this can be accomplished using interfaces, as a class can implement multiple interfaces. Remember: To implement multiple interfaces, list them with commas (as illustrated below).

Multiple Interfaces

To implement multiple interfaces, list them separated by commas.

Example

interface FirstInterface {
  public void myMethod(); // interface method
}                                   
 
interface SecondInterface {
  public void myOtherMethod(); // interface method
}
 
// DemoClass "implements" FirstInterface and SecondInterface
class DemoClass implements FirstInterface, SecondInterface {
  public void myMethod() {
    System.out.println("Some text..");
  }
  public void myOtherMethod() {
    System.out.println("Some other text...");
  }
}
 
class MyMainClass {
  public static void main(String[] args) {
    DemoClass myObj = new DemoClass();
    myObj.myMethod();
    myObj.myOtherMethod();
  }
}