Java Basics  «Prev  Next»

Lesson 7Interfaces
ObjectiveDefine role of Java Interfaces

Define Role of the Java interface

  1. Describe the role of an interface and identify when to use one.
  2. Implement an interface.

Java allows multiple inheritance of interface, but not multiple inheritance of implementation. You can define an interface in Java to take advantage of multiple inheritance of interface. Interfaces provide a mechanism for your classes to share characteristics. Interfaces are used most often when you want to assign behavior to classes that do not all descend from the same superclass, and when this behavior needs to be redefined for each object that implements a particular interface.
You define an interface similarly to how you define a class. However, all variables in an interface are class variables and constants, and all methods are abstract.
Here's an example from the subatomic world (for more information on subatomics, check out this link to Fermi Labs

Define an Interface and have both Parent class and Subclass implement that interface

In Java, you can define an interface and have both a parent class and a subclass implement that interface. Since interfaces allow multiple classes to share a contract (without implementation inheritance), both the parent class (`Animal`) and the subclass (`Mammal`) can provide their own implementations of the interface's methods.
Here's an example:
// Interface `LivingBeing`
interface LivingBeing {
    void eat();
    void move();
}
// Parent Class `Animal`
class Animal implements LivingBeing {
    @Override
    public void eat() {
        System.out.println("Animal is eating.");
    }
    
    @Override
    public void move() {
        System.out.println("Animal is moving.");
    }
}
// Subclass `Mammal`
class Mammal extends Animal {
    @Override
    public void eat() {
        System.out.println("Mammal is eating.");
    }
    
    @Override
    public void move() {
        System.out.println("Mammal is moving.");
    }
    
    public void feedYoung() {
        System.out.println("Mammal is feeding its young.");
    }
}


//Main Method Example:
public class Main {
    public static void main(String[] args) {
        Animal animal = new Animal();
        animal.eat();  // Outputs: Animal is eating.
        animal.move(); // Outputs: Animal is moving.
        
        Mammal mammal = new Mammal();
        mammal.eat();  // Outputs: Mammal is eating.
        mammal.move(); // Outputs: Mammal is moving.
        mammal.feedYoung(); // Outputs: Mammal is feeding its young.
    }
}

Explanation:
  • LivingBeing is the interface that defines the contract (`eat` and `move` methods).
  • Animal is a parent class that implements `LivingBeing` and provides basic implementations for `eat` and `move`.
  • Mammal extends `Animal`, and overrides the `eat` and `move` methods to provide its own specific behavior. Additionally, it has a new method `feedYoung`, which is unique to mammals.

Both `Animal` and `Mammal` implement the `LivingBeing` interface, ensuring they both fulfill the contract defined by the interface.


Arrays of type interface, abstract class, and class Object

The type of an array can also be an interface or an abstract class. What values do elements of these arrays store?
Let us take a look at some examples.
  • Interface Type If the type of an array is an interface, its elements are either null or objects that implement the relevant interface type. For example, for the interface MyInterface, the array interfaceArray can store references to objects of either the class MyClass1 or MyClass2:
    interface MyInterface {}
    class MyClass1 implements MyInterface {}
    class MyClass2 implements MyInterface {}
    class Test {
      MyInterface[] interfaceArray = new MyInterface[]{
        new MyClass1(),
        null,
        new MyClass2()
      };
    }
    

interface ElectricallyCharged {
  int units = 3; // electric charge is charge()/units
  int charge();
}
// ----- TopQuark
class TopQuark implements ElectricallyCharged {
 public int charge() {
 return 2;
 }
}

Notice that charge() is defined as public in the class definition for TopQuark. This is because methods are implicitly defined as public in an interface.
Just as we've done for TopQuark, we could make other particles electrically charged by indicating that they immplement this interface.

Implementing Interface - Exercise

To play around with this concept of interfaces, complete an exercise that asks you to implement one of Java's interfaces.
Implementing Interface - Exercise

Java Interfaces - Quiz

The following quiz asks a few questions about interfaces.
Java Interfaces - Quiz

SEMrush Software