Home >Java >javaTutorial >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.
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.
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.
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!