DevAcademia
C++C#CPythonJava
  • Java Basics

  • Java Introduction
  • Java Get Started
  • Java Syntax
  • Java Output
  • Java Comments
  • Java Variables
  • Java Data Types
  • Java Type Casting
  • Java Operators
  • Java Strings
  • Java If...Else
  • Java Switch Statement
  • Java Loops
  • Java Math
  • Java Arrays
  • Java Date
  • Java OOP

  • Java Classes/Objects
  • Java Class Attributes
  • Java Class Methods
  • Java Constructors
  • Java Destructors
  • Java this Keyword
  • Java Modifiers
  • Java Non Modifiers
  • Java Encapsulation
  • Java Packages & API
  • Java Inheritance
  • Java Polymorphism
  • Java Super Keyword
  • Java Inner Classes
  • Java Exception Handling
  • Java Abstraction
  • Java Interfaces
  • Java Enums
  • Java User Input
  • Java Quiz

  • Java Fundamentals Quiz
  • Java Basics

  • Java Introduction
  • Java Get Started
  • Java Syntax
  • Java Output
  • Java Comments
  • Java Variables
  • Java Data Types
  • Java Type Casting
  • Java Operators
  • Java Strings
  • Java If...Else
  • Java Switch Statement
  • Java Loops
  • Java Math
  • Java Arrays
  • Java Date
  • Java OOP

  • Java Classes/Objects
  • Java Class Attributes
  • Java Class Methods
  • Java Constructors
  • Java Destructors
  • Java this Keyword
  • Java Modifiers
  • Java Non Modifiers
  • Java Encapsulation
  • Java Packages & API
  • Java Inheritance
  • Java Polymorphism
  • Java Super Keyword
  • Java Inner Classes
  • Java Exception Handling
  • Java Abstraction
  • Java Interfaces
  • Java Enums
  • Java User Input
  • Java Quiz

  • Java Fundamentals Quiz

Loading Java tutorial…

Loading content
Java OOPTopic 56 of 59
←PreviousPrevNextNext→

Java Interface

Introduction to Interfaces

An interface in Java is a reference type that contains only abstract methods (until Java 8), default methods, static methods, and constant declarations.

Interfaces define a contract that implementing classes must follow. They provide complete abstraction as all methods are abstract by default (before Java 8).

Defining and Implementing Interfaces

Interfaces are declared using the 'interface' keyword, and classes implement them using the 'implements' keyword.

A class can implement multiple interfaces, enabling a form of multiple inheritance.

Example
// Interface definition
interface Animal {
    void makeSound(); // Abstract method (public abstract by default)
    void eat();       // Abstract method
}

// Another interface
interface Pet {
    void play();
}

// Class implementing multiple interfaces
class Dog implements Animal, Pet {
    @Override
    public void makeSound() {
        System.out.println("Woof!");
    }
    
    @Override
    public void eat() {
        System.out.println("Eating dog food");
    }
    
    @Override
    public void play() {
        System.out.println("Playing fetch");
    }
}

public class InterfaceExample {
    public static void main(String[] args) {
        Dog myDog = new Dog();
        myDog.makeSound();
        myDog.eat();
        myDog.play();
        
        // Using interface reference
        Animal animal = new Dog();
        animal.makeSound();
    }
}
Output
Woof!
Eating dog food
Playing fetch
Woof!

Interface Features (Java 8+)

Since Java 8, interfaces can contain:

- Default methods (with implementation)

- Static methods (with implementation)

- Private methods (Java 9+)

Example
interface Calculator {
    // Abstract method
    int calculate(int a, int b);
    
    // Default method (Java 8+)
    default void displayResult(int result) {
        System.out.println("Result: " + result);
    }
    
    // Static method (Java 8+)
    static String getCalculatorType() {
        return "Basic Calculator";
    }
}

class Adder implements Calculator {
    @Override
    public int calculate(int a, int b) {
        return a + b;
    }
}

public class ModernInterfaceExample {
    public static void main(String[] args) {
        Adder adder = new Adder();
        int result = adder.calculate(5, 3);
        adder.displayResult(result);
        
        // Calling static method
        System.out.println(Calculator.getCalculatorType());
    }
}
Output
Result: 8
Basic Calculator

Interface vs Abstract Class

FeatureInterfaceAbstract Class
MethodsAll abstract (before Java 8)Mix of abstract and concrete
VariablesOnly public static finalAny type of variables
InheritanceMultiple inheritance supportedSingle inheritance only
ConstructorNo constructorsCan have constructors
Access ModifiersMethods are public by defaultMethods can have any access modifier

Functional Interfaces

A functional interface is an interface with exactly one abstract method. They are the basis for lambda expressions in Java.

Example
// Functional interface (only one abstract method)
@FunctionalInterface
interface MathOperation {
    int operate(int a, int b);
    
    // Can have default methods
    default void printResult(int result) {
        System.out.println("Operation result: " + result);
    }
}

public class FunctionalInterfaceExample {
    public static void main(String[] args) {
        // Using lambda expression
        MathOperation addition = (a, b) -> a + b;
        MathOperation multiplication = (a, b) -> a * b;
        
        int sum = addition.operate(5, 3);
        int product = multiplication.operate(5, 3);
        
        addition.printResult(sum);
        multiplication.printResult(product);
    }
}
Output
Operation result: 8
Operation result: 15

Best Practices

- Use interfaces to define contracts that multiple classes can implement

- Prefer interfaces over abstract classes when possible for flexibility

- Use the @FunctionalInterface annotation for single-method interfaces

- Keep interfaces focused and cohesive (Interface Segregation Principle)

- Use default methods for backward compatibility when extending interfaces

Test your knowledge: Java Interface
Quiz Configuration
4 of 8 questions
Sequential
Previous allowed
Review enabled
Early close allowed
Estimated time: 5 min
Java OOPTopic 56 of 59
←PreviousPrevNextNext→