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

Java Interface

Interfaces

Another approach to attain abstraction in Java is through interfaces.

An interface serves as a wholly “abstract class” employed to gather related methods with unimplemented bodies.

Example

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

To utilize the methods defined in an interface, another class must “implement” it, akin to inheritance but using the implements keyword instead of extends. The implementing class provides the implementation for the methods defined in the 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();  // Create a Pig object
    myPig.animalSound();
    myPig.sleep();
  }
}

 

Notes on Interfaces:

  • Similar to abstract classes, interfaces cannot instantiate objects. For instance, in the example provided, creating an “Animal” object in MyMainClass is not feasible.
  • Interface methods lack a body, and it is the implementing class that furnishes their implementation.
  • When implementing an interface, it is mandatory to override all its methods.
  • By default, interface methods are abstract and public.
  • Interface attributes are public, static, and final.
  • Interfaces cannot include constructors since they cannot instantiate objects.

Why And When To Use Interface?

  1. To enhance security, conceal certain details and selectively expose essential aspects of an object using interfaces.
  2. Java does not allow “multiple inheritance” (where a class inherits from more than one superclass). However, this functionality can be achieved through interfaces, as a class can implement multiple interfaces.

Note: When implementing multiple interfaces, separate them with commas, as shown in the example below.

Multiple Interfaces

To implement multiple interfaces, use commas to separate them. 

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...");
  }
}

 class Main {
  public static void main(String[] args) {
   DemoClass myObj = new DemoClass();
   myObj.myMethod();
  myObj.myOtherMethod();
}
}