Baidu interview question: Why use interfaces instead of using concrete classes directly?

Hello everyone, I am Xiaomi! Today, I want to talk to you about a very important topic in Java programming: "Baidu interview question: Why use interfaces instead of using concrete classes directly?" This question will be asked in many Java interviews because it involves Reached the core ideas of object-oriented programming and the characteristics of the Java language.

Before we begin, let us first understand what an interface (Interface) and a concrete class (Concrete Class) are.

Interface

An interface is an abstract data type in Java that defines a set of abstract methods but has no implementation of the methods. An interface is declared using the interface keyword. Its main function is to define which methods a class should have, but does not provide specific implementation of the methods . When a class implements an interface, it must implement all methods defined in the interface.

Concrete Class

A concrete class refers to a class that can be instantiated and can contain specific implementations of properties and methods. Concrete classes can inherit other classes or implement interfaces, and must provide concrete implementations of all inherited or implemented methods .

Now let us delve into why we use interfaces in Java instead of using concrete classes directly. This question involves some core concepts and programming principles of object-oriented programming.

Function 1: Polymorphism

Interfaces allow us to achieve polymorphism, an important concept in object-oriented programming. Polymorphism allows us to use an interface reference to reference objects of different concrete classes, thereby achieving code flexibility and scalability .

For example, we can define an interface Animal , and then let different animal classes such as dogs, cats, birds, etc. implement this interface. In this way, we can create a general method that accepts a reference to the Animal interface, and then calls methods of different animal classes to achieve polymorphism.

This polymorphism allows us to easily extend the code and add new concrete classes without having to modify the existing code .

Function 2: Decoupling

Using interfaces can help us decouple our code. Decoupling refers to reducing the dependencies between codes so that each part can be modified and maintained independently . When we use concrete classes, the dependencies between codes tend to be higher because they depend directly on the concrete implementation.

However, when we use interfaces, we only care about the methods defined by the interface, not the implementation details of the specific class. This means that we can easily replace concrete classes as long as they implement the same interface without having to modify other code that depends on the interface.

Function three: code reuse

Interfaces can also help us achieve code reuse. By defining a common interface, we can implement the same method in different concrete classes, thereby improving code reusability . This is because multiple concrete classes can share the same interface without having to rewrite the same code.

Role 4: Implement specifications

Interfaces can also be used to define a set of specifications for different concrete classes to follow. This ensures that different classes implement the same methods to meet specific requirements or conventions .

For example, in Java, there is a Serializable interface that marks a class as being serializable, which means that an object can be converted into a sequence of bytes for storage or transmission. If a class implements the Serializable interface, then it must implement the corresponding methods to meet the Java serialization specifications.

Function 5: Interface flexibility

Interfaces allow us to introduce new functionality without changing the inheritance structure of the class . This is because a class can implement multiple interfaces and thus obtain multiple different functions.

Suppose we have a Car class that implements a Vehicle interface and an Engine interface. This Car class can be regarded as both a vehicle and an object with an engine, which increases the flexibility of the code.

Conclusion

Overall, using interfaces instead of using concrete classes directly is a good programming practice that enables polymorphism, decoupling, code reuse, discipline, and flexibility . By defining common interfaces, we can write more flexible, maintainable and extensible code , thereby improving development efficiency and code quality.

Of course, this does not mean that interfaces should be used in all cases, and there are some cases where it is reasonable to use concrete classes . Choosing to use interfaces or concrete classes depends on specific needs and design goals. In actual development, we usually make flexible choices according to the situation, and use interfaces reasonably as needed to improve the quality and maintainability of the code.

I hope that through this article, everyone will have a deeper understanding of why interfaces are used. If you have any questions or want to share your opinions, please feel free to leave a message in the comment area, I am very happy to interact with you! Thank you everyone for reading!

If you have any questions or more technical sharing, please follow my WeChat public account " Know what it is and why "!

Guess you like

Origin blog.csdn.net/en_joker/article/details/133383274