Home  >  Article  >  Java  >  The role of interfaces and abstract classes in Java generics

The role of interfaces and abstract classes in Java generics

WBOY
WBOYOriginal
2024-05-02 15:00:02376browse

Interface is used to define the behavior that a class must implement, allowing subtyping. Abstract classes contain abstract methods, allowing polymorphism. Abstract classes cannot be instantiated.

接口和抽象类在 Java 泛型中的作用

The role of interfaces and abstract classes in Java generics

Introduction
Generics Is a powerful programming technique that allows us to create reusable code without specifying specific types. Interfaces and abstract classes play a crucial role in generics, providing mechanisms to achieve polymorphism and abstraction in typed systems.

Interface
An interface is an abstract type that only contains method declarations. It defines a set of methods that must be implemented by any class that implements the interface. An interface cannot contain any fields or constructors.

Subtyping
The interface can be used to establish subtyping relationships. When a class implements an interface, it becomes a subtype of that interface. This means that an object of that class can be assigned to that interface type variable, just like a child class object can be assigned to its parent class type variable.

Practical example

public interface Comparable<T> {
    int compareTo(T other);
}

public class Person implements Comparable<Person> {

    private String name;
    private int age;

    @Override
    public int compareTo(Person other) {
        return this.age - other.age;
    }
}

// 使用示例
List<Person> people = new ArrayList<>();
people.add(new Person("John", 30));
people.add(new Person("Mary", 25));

Collections.sort(people);  // 使用接口实现按年龄排序

In this example, the Person class implements the Comparable interface so that it can be sorted by age Compare. This means we can sort a collection of Person objects using the Collections.sort() method.

Abstract class
An abstract class is a class that cannot be instantiated. It can contain abstract methods (methods without implementation), as well as ordinary methods and fields.

Polymorphism
Abstract classes allow objects to be used in a polymorphic manner. When you create a variable that refers to an abstract class type, it can refer to an object of that class or any of its subclasses.

Practical example

public abstract class Shape {

    private String color;

    public abstract double getArea();
}

public class Rectangle extends Shape {

    private double width;
    private double height;

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

public class Circle extends Shape {

    private double radius;

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

// 使用示例
List<Shape> shapes = new ArrayList<>();
shapes.add(new Rectangle(5, 10));
shapes.add(new Circle(5));

for (Shape shape : shapes) {
    System.out.println(shape.getArea());
}

In this example, Shape is an abstract class that defines a getArea() Method implemented by its subclasses Rectangle and Circle. This allows us to create objects of various shapes and store them in a collection and use them in a polymorphic way.

The above is the detailed content of The role of interfaces and abstract classes in Java generics. 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