Home >Java >javaTutorial >Polymorphism application of interfaces and abstract classes in Java

Polymorphism application of interfaces and abstract classes in Java

PHPz
PHPzOriginal
2024-05-02 10:33:011202browse

In Java, polymorphism is achieved through interfaces and abstract classes. An interface defines methods that must be implemented by the class that implements it; an abstract class contains abstract methods that must be implemented by its subclasses. Polymorphism is achieved by creating an array of objects and calling the same method, allowing different implementations to be called depending on the actual type of the element. For example, you can define a Shape interface to define the drawing methods of the Shape class, then create multiple shape classes (such as Circle, Square, Triangle) with different implementations, and use polymorphism to draw all the shapes through a canvas class.

Java 中接口和抽象类的多态性应用

Polymorphism application of interfaces and abstract classes in Java

Polymorphism is the key to object-oriented programming (OOP) Key concept that allows objects to respond to the same method call in different ways. In Java, polymorphism can be achieved through interfaces and abstract classes.

Interface

An interface defines a set of methods that must be implemented by the class that implements it. The interface itself does not contain any implementation, it just specifies the contract.

public interface Shape {
    double getArea();
}

Abstract class

An abstract class is a class that has at least one abstract method. Abstract methods are not implemented and must be implemented by their subclasses. Abstract classes can contain concrete methods and fields.

public abstract class Animal {
    protected String name;
    
    public abstract void makeSound();
}

Polymorphism

Using interfaces and abstract classes, it is possible to create arrays of objects in which each element has a different type. When the same method on these elements is called, a different implementation is called depending on the type of the actual element.

Shape[] shapes = { new Circle(), new Square(), new Triangle() };
for (Shape shape : shapes) {
    System.out.println("Area: " + shape.getArea());
}

Practical case

Consider a canvas class that can draw different shapes. Shapes can be defined using the following interface:

public interface Shape {
    void draw();
}

Then, multiple shape classes with different drawing implementations can be created:

public class Circle implements Shape {
    @Override
    public void draw() { System.out.println("Drawing a circle"); }
}

public class Square implements Shape {
    @Override
    public void draw() { System.out.println("Drawing a square"); }
}

public class Triangle implements Shape {
    @Override
    public void draw() { System.out.println("Drawing a triangle"); }
}

The canvas class can accept an array of Shape objects, using polymorphism to draw all Shape:

public class Canvas {
    public void drawAll(Shape[] shapes) {
        for (Shape shape : shapes) {
            shape.draw();
        }
    }
}

Through interfaces and abstract classes, polymorphism in Java allows methods to be called dynamically based on the actual type of the object, thus enabling scalable and reusable code.

The above is the detailed content of Polymorphism application of interfaces and abstract classes 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

Related articles

See more