J

Interfaces

Java Syntax Guide

Definieren von Verträgen mit Interfaces in Java

Interfaces

Definieren von Verträgen mit Interfaces in Java

Java interfaces (java)
        
          // Interface definition
interface Drawable {
    void draw();
    default void setColor(String color) {
        System.out.println("Setting color to " + color);
    }
}

interface Resizable {
    void resize(double factor);
    boolean canResize();
}

// Class implementing multiple interfaces
class Rectangle implements Drawable, Resizable {
    private double width, height;
    private String color;

    public Rectangle(double width, double height) {
        this.width = width;
        this.height = height;
        this.color = "Black";
    }

    @Override
    public void draw() {
        System.out.println("Drawing rectangle: " + width + "x" + height);
    }

    @Override
    public void setColor(String color) {
        this.color = color;
        System.out.println("Rectangle color set to " + color);
    }

    @Override
    public void resize(double factor) {
        width *= factor;
        height *= factor;
    }

    @Override
    public boolean canResize() {
        return true;
    }
}

// Polymorphism in action
public class GraphicsDemo {
    // Method accepting interface type
    public static void renderShape(Drawable shape) {
        shape.draw();
        shape.setColor("Blue");
    }

    public static void resizeIfPossible(Resizable resizable, double factor) {
        if (resizable.canResize()) {
            resizable.resize(factor);
            System.out.println("Resized by factor: " + factor);
        }
    }

    public static void main(String[] args) {
        Rectangle rect = new Rectangle(10, 5);

        // Polymorphic behavior
        renderShape(rect);        // Drawable behavior
        resizeIfPossible(rect, 2.0); // Resizable behavior

        // Array of interfaces
        Drawable[] shapes = new Drawable[] {
            new Rectangle(5, 3),
            new Circle(4)
        };

        for (Drawable shape : shapes) {
            shape.draw();
        }
    }
}

// Functional interface (Java 8+)
@FunctionalInterface
interface Calculator {
    int calculate(int a, int b);
}

class MathOperations {
    public static void main(String[] args) {
        // Lambda expressions
        Calculator add = (a, b) -> a + b;
        Calculator multiply = (a, b) -> a * b;
        Calculator subtract = (a, b) -> a - b;

        System.out.println("5 + 3 = " + add.calculate(5, 3));
        System.out.println("5 * 3 = " + multiply.calculate(5, 3));
        System.out.println("5 - 3 = " + subtract.calculate(5, 3));

        // Method reference
        Calculator power = MathOperations::powerOfTwo;
        System.out.println("2^3 = " + power.calculate(2, 3));
    }

    private static int powerOfTwo(int base, int exponent) {
        return (int) Math.pow(base, exponent);
    }
}
        
      

Explanation

Interfaces in Java definieren einen Vertrag, den Klassen implementieren sollen. Sie können abstrakte Methoden (ohne Implementierung) und Default-Methoden (mit Implementierung) enthalten. Eine Klasse kann mehrere Interfaces implementieren.

Common Use Cases

  • Erreichen von Abstraktion
  • Unterstützen mehrerer Vererbung von Typ
  • Definieren von API-Verträgen

Related Java Syntax

Master Interfaces in Java

Understanding Interfaces is fundamental to writing clean and efficient Java code. This comprehensive guide provides you with practical examples and detailed explanations to help you master this important concept.

Whether you're a beginner learning the basics or an experienced developer looking to refresh your knowledge, our examples cover real-world scenarios and best practices for using Interfaces effectively in your Java projects.

Key Takeaways

  • Erreichen von Abstraktion
  • Unterstützen mehrerer Vererbung von Typ
  • Definieren von API-Verträgen