Home  >  Article  >  Java  >  Differences between Java interfaces and classes: Polymorphism and flexibility

Differences between Java interfaces and classes: Polymorphism and flexibility

WBOY
WBOYOriginal
2024-01-11 12:26:42815browse

Differences between Java interfaces and classes: Polymorphism and flexibility

The difference between Java interfaces and classes: polymorphism and flexibility

Java is an object-oriented programming language, and interfaces and classes are one of its important concepts one. Interfaces and classes have different uses and characteristics in Java. This article will introduce the differences between interfaces and classes from the aspects of polymorphism and flexibility, and provide specific example code to illustrate.

1. Polymorphism:
Polymorphism is one of the core concepts of object-oriented programming, which refers to the fact that objects of the same type have different behavioral characteristics. In Java, both interfaces and classes can achieve polymorphism, but the way they are implemented is different.

  1. Polymorphism of classes:
    Polymorphism of classes is achieved through inheritance and overwriting. After a subclass inherits a parent class, it can override the parent class's methods to change the behavior of the methods. When the program is executed, polymorphism can be achieved by pointing to the subclass object through the reference of the parent class.

The sample code is as follows:

class Animal{
    void sound(){
        System.out.println("动物发出声音");
    }
}

class Dog extends Animal{
    void sound(){
        System.out.println("狗发出汪汪声");
    }
}

class Cat extends Animal{
    void sound(){
        System.out.println("猫发出喵喵声");
    }
}

public class PolymorphismTest {
    public static void main(String[] args) {
        Animal animal = new Animal();
        Animal dog = new Dog();
        Animal cat = new Cat();

        animal.sound();
        dog.sound();
        cat.sound();
    }
}

Output result:

动物发出声音
狗发出汪汪声
猫发出喵喵声

In the above code, the Animal class is the parent class, and the Dog and Cat classes are children of the Animal class kind. In the main method, Animal, Dog and Cat objects are created respectively, and the sound() method of the corresponding subclass is called through the reference of the parent class. Due to the existence of overriding, the actual results obtained when calling the sound() method of different objects are also different. This reflects the polymorphism of the class.

  1. Polymorphism of interfaces:
    Polymorphism of interfaces is achieved by implementing interfaces and references to interfaces. A class that implements an interface must implement all methods defined in the interface to achieve polymorphism.

The sample code is as follows:

interface Animal{
    void sound();
}

class Dog implements Animal{
    public void sound(){
        System.out.println("狗发出汪汪声");
    }
}

class Cat implements Animal{
    public void sound(){
        System.out.println("猫发出喵喵声");
    }
}

public class PolymorphismTest {
    public static void main(String[] args) {
        Animal dog = new Dog();
        Animal cat = new Cat();

        dog.sound();
        cat.sound();
    }
}

Output result:

狗发出汪汪声
猫发出喵喵声

In the above code, Animal is the interface, and Dog and Cat are classes that implement the Animal interface. In the main method, the references through the Animal interface point to the Dog and Cat objects respectively, and the sound() method is called. Similarly, because the methods in the interface are implemented differently in different classes, you will get different results when you call the method. This reflects the polymorphism of the interface.

2. Flexibility:
Flexibility refers to whether modifications and extensions to implementation details can be easily implemented in program design. The difference in flexibility between interfaces and classes is mainly reflected in the following two aspects.

  1. Flexibility of classes:
    The flexibility of classes is mainly reflected through inheritance and polymorphism. When you need to modify a class, you only need to modify the subclass, which will not affect other codes that use the class. This flexibility achieved through inheritance makes the code more extensible.
  2. Flexibility of the interface:
    The flexibility of the interface is mainly reflected in the expansion and modification at the interface level. When requirements change, only the interface needs to be modified, without modifying the class that implements the interface. This flexibility achieved through interfaces makes programs easier to maintain and expand.

To sum up, although both interfaces and classes can achieve polymorphism, they are different in their uses and characteristics. Classes achieve polymorphism mainly through inheritance and overriding, and interfaces achieve polymorphism by implementing interfaces and references to interfaces. In terms of flexibility, classes are mainly realized through inheritance and polymorphism, while interfaces are mainly realized through extension and modification at the interface level. Therefore, in actual applications, the use of interfaces and classes needs to be weighed as needed to achieve better software design results.

The above is the detailed content of Differences between Java interfaces and classes: Polymorphism and flexibility. 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