Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> Java

Java Interfaces Explained: How to Define and Implement Them with Practical Examples

What Is a Java Interface?

A Java interface is an abstract type that defines a contract for classes. It can declare constants and abstract methods but cannot hold state. All methods are implicitly public and abstract, and a class can implement multiple interfaces, providing the required behavior.

Declaring an Interface

interface MyInterface {
    // abstract methods
}

Implementing an Interface

class Dog implements Pet {
    // implement methods from Pet
}

Extending Interfaces

interface RidableAnimal extends Animal, Vehicle {
    // additional abstract methods
}

Why Are Interfaces Needed?

Interfaces allow you to separate the what from the how. They enable polymorphism, promote loose coupling, and solve the “multiple inheritance” problem that Java does not support for classes.

For example, a MediaPlayer may have separate CDPlayer and DVDPlayer subclasses, each providing its own play() implementation. If a class needed both capabilities, Java would forbid multiple inheritance of classes, but an interface lets us combine the behaviors cleanly.

Java Interfaces Explained: How to Define and Implement Them with Practical Examples

Java Interfaces Explained: How to Define and Implement Them with Practical Examples

Similarly, a Dog class can extend Animal and implement a Pet interface to satisfy both inheritance and contractual obligations.

Java Interfaces Explained: How to Define and Implement Them with Practical Examples

Java Interface Example

Step 1: Write the following code:

interface Pet {
    void test();
}

class Dog implements Pet {
    @Override
    public void test() {
        System.out.println("Interface method implemented");
    }

    public static void main(String[] args) {
        Pet p = new Dog();
        p.test();
    }
}

Step 2: Compile and run the program to see the output.

Class vs. Interface

ClassInterface
A class can be instantiated and may contain state.An interface cannot be instantiated and contains only constants and method signatures.
Can have concrete (implemented) methods.Before Java 8, could not have concrete methods; from Java 8 onwards, may contain default and static methods.
Access modifiers can be private, protected, or public.All members are implicitly public (and static final for fields).

When to Use an Interface vs. an Abstract Class?

Key Facts About Interfaces

Summary

Java

  1. Understanding C# Interfaces: Definition, Example, and Practical Use
  2. Understanding Java Classes and Objects: Clear Concepts, Practical Examples
  3. Java String length() Method: How to Get a String’s Size (Example)
  4. Mastering the 'this' Keyword in Java: Purpose, Usage, and Practical Examples
  5. Polymorphism in Java: A Comprehensive Guide with Practical Examples
  6. Java Abstraction: Mastering Abstract Classes, Methods, and Practical Examples
  7. Interface vs Abstract Class in Java: How to Choose the Right Abstraction
  8. Mastering Java's split() Method: A Practical Guide with Code Examples
  9. Reading Files in Java with BufferedReader – A Practical Guide with Examples
  10. Master Java Reflection API: A Practical Guide with Code Examples