Home  >  Article  >  Java  >  Implementation methods and sample codes of interfaces in Java

Implementation methods and sample codes of interfaces in Java

PHPz
PHPzOriginal
2023-12-23 09:21:491368browse

Implementation methods and sample codes of interfaces in Java

Implementation methods and sample codes of interfaces in Java

Introduction: In the Java programming language, an interface is a special abstract class that defines a set of methods signature but not implemented. Interfaces can be used to define the requirements of a class, and these requirements are implemented in the implementation class.

How to define the interface:
In Java, the interface is defined through the keyword "interface". Interfaces can define constants and methods, but they cannot contain instance variables. Methods in interfaces default to public abstract, and constants default to public static final.

How to implement the interface:
In Java, there are two ways to implement the interface: using the implements keyword and using anonymous inner classes.

Method 1: Use the implements keyword
Using the implementation keyword implements, a class can implement one or more interfaces.

// 定义一个接口
public interface Shape {
    double getArea();
    double getPerimeter();
}

// 定义一个实现类
public class Circle implements Shape {
    private double radius;

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

    @Override
    public double getArea() {
        return Math.PI * radius * radius;
    }

    @Override
    public double getPerimeter() {
        return 2 * Math.PI * radius;
    }
}

// 测试类
public class Main {
    public static void main(String[] args) {
        Circle circle = new Circle(5);
        System.out.println("圆的面积: " + circle.getArea());
        System.out.println("圆的周长: " + circle.getPerimeter());
    }
}

Method 2: Use anonymous inner classes
Using anonymous inner classes can instantiate the class while implementing the interface.

// 定义一个接口
public interface Shape {
    double getArea();
    double getPerimeter();
}

// 测试类
public class Main {
    public static void main(String[] args) {
        // 使用匿名内部类实现接口
        Shape triangle = new Shape() {
            private double base = 4;
            private double height = 3;

            @Override
            public double getArea() {
                return 0.5 * base * height;
            }

            @Override
            public double getPerimeter() {
                return base + 2 * Math.sqrt(Math.pow(base / 2, 2) + Math.pow(height, 2));
            }
        };

        System.out.println("三角形的面积: " + triangle.getArea());
        System.out.println("三角形的周长: " + triangle.getPerimeter());
    }
}

Conclusion: In Java, interfaces are a way of defining requirements, which can make the code more flexible and extensible. By using the implements keyword or anonymous inner class to implement the interface, the behavior of the class can be customized according to specific needs.

Write complete code examples to help understand the usage and flexibility of the interface. At the same time, please note that when implementing an interface, you must implement all methods declared in the interface, otherwise compilation errors will occur. Through reasonable use of interfaces, the readability and maintainability of the code can be improved.

The above is the detailed content of Implementation methods and sample codes of interfaces in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn