Java Tutorial

Operators

Flow Control

String

Number and Date

Built-in Classes

Array

Class and Object

Inheritance and Polymorphism

Exception Handling

Collections, Generics and Enumerations

Reflection

Input/Output Stream

Annotation

Java Interface

An interface in Java is a collection of abstract methods (methods without a body) that can be implemented by any class. Interfaces are used to define a contract or a behavior that classes must adhere to. In this tutorial, we'll discuss the basics of interfaces in Java, including how to create, implement, and use them.

  • Creating an Interface

To create an interface in Java, use the interface keyword followed by the name of the interface. Inside the interface, declare the abstract methods that classes implementing the interface must define.

Example:

public interface Drawable {
    void draw();
}

In this example, we create an interface called Drawable with a single abstract method named draw.

  • Implementing an Interface

To implement an interface, a class must use the implements keyword followed by the name of the interface. The class must then provide implementations for all the abstract methods declared in the interface.

Example:

public class Circle implements Drawable {
    private double radius;

    public Circle(double radius) {
        this.radius = radius;
    }

    @Override
    public void draw() {
        System.out.println("Drawing a circle with radius " + radius);
    }
}

In this example, we create a class called Circle that implements the Drawable interface. The Circle class provides an implementation for the draw method declared in the Drawable interface.

  • Using an Interface

An interface can be used as a reference type for objects of implementing classes. This allows you to write more flexible and modular code, as you can change the underlying implementation without affecting the rest of your program.

Example:

public class Main {
    public static void main(String[] args) {
        Drawable drawable = new Circle(5);
        drawable.draw(); // Drawing a circle with radius 5.0
    }
}

In this example, we create a Drawable reference called drawable and assign it an instance of the Circle class. Since the Circle class implements the Drawable interface, we can call the draw method using the drawable reference.

  • Default and Static Methods in Interfaces

Java 8 introduced default and static methods in interfaces, allowing you to provide a default implementation for methods and define static utility methods within interfaces.

Example:

public interface Drawable {
    void draw();

    default void printDrawingInfo() {
        System.out.println("Drawing a shape.");
    }

    static void printDrawingInstructions() {
        System.out.println("Use a pencil and paper to draw the shape.");
    }
}

public class Circle implements Drawable {
    // ... (same as before)

    @Override
    public void printDrawingInfo() {
        System.out.println("Drawing a circle with radius " + radius);
    }
}

public class Main {
    public static void main(String[] args) {
        Drawable drawable = new Circle(5);
        drawable.draw(); // Drawing a circle with radius 5.0
        drawable.printDrawingInfo(); // Drawing a circle with radius 5.0
        Drawable.printDrawingInstructions(); // Use a pencil and paper to draw the shape.
    }
}

In this example, we add a default method printDrawingInfo and a static method printDrawingInstructions to the Drawable interface. The Circle class can override the default method, and the static method can be called directly on the interface.

In conclusion, interfaces in Java provide a powerful way to define contracts or behaviors that classes must adhere to. By understanding the basics of interfaces and how to create, implement, and use them, you can write cleaner, more modular code that is easier to maintain and extend.

  1. Defining and implementing interfaces in Java

    // Interface definition
    interface Greetable {
        void greet();
    }
    
    // Implementation
    class Greeter implements Greetable {
        @Override
        public void greet() {
            System.out.println("Hello, World!");
        }
    }
    
  2. Multiple inheritance with Java interfaces

    interface Walkable {
        void walk();
    }
    
    interface Swimmable {
        void swim();
    }
    
    class Human implements Walkable, Swimmable {
        @Override
        public void walk() {
            System.out.println("Walking");
        }
    
        @Override
        public void swim() {
            System.out.println("Swimming");
        }
    }
    
  3. Default methods in Java interfaces

    interface Greetable {
        default void greet() {
            System.out.println("Hello!");
        }
    }
    
    // Class implementing the interface
    class Greeter implements Greetable {
        // No need to override the default method
    }
    
  4. Static methods in Java interfaces

    interface MathOperations {
        static int add(int a, int b) {
            return a + b;
        }
    }
    
    // Using static method
    int result = MathOperations.add(2, 3);
    
  5. Marker interfaces in Java

    Marker interfaces have no methods, they act as a tag to inform something about the implementing class.

    interface SerializableMarker { }
    
    class Data implements SerializableMarker, SomeInterface {
        // Implementing interfaces
    }
    
  6. Extending interfaces in Java

    interface Animal {
        void makeSound();
    }
    
    interface Mammal extends Animal {
        void giveBirth();
    }
    
    class Dog implements Mammal {
        @Override
        public void makeSound() {
            System.out.println("Bark");
        }
    
        @Override
        public void giveBirth() {
            System.out.println("Giving birth to puppies");
        }
    }
    
  7. Using interfaces for callback functions in Java

    interface Callback {
        void onComplete();
    }
    
    class Task {
        void execute(Callback callback) {
            // Performing task
            callback.onComplete();
        }
    }
    
    // Usage
    Task task = new Task();
    task.execute(() -> System.out.println("Task completed!"));