Detailed explanation of interface and abstract class usage examples in Java

This article describes the usage of interfaces and abstract classes in Java. Share with you for your reference, as follows:

In the concept of object-oriented, we know that all objects are described by classes, but not all classes are used to describe objects. If a class does not contain enough information to describe a specific object, such a class is an abstract class.

Abstract classes are often used to represent the abstract concepts we get in the analysis and design of the problem field. They are abstractions of a series of concrete concepts that look different but are essentially the same. We can't instantiate them (we can't get a concrete thing), so they are called abstractions.

For example, we want to describe "fruit", which is an abstraction. It has quality Some commonalities such as volume (fruits have quality), but they lack characteristics (apples and oranges are fruits, and they have their own characteristics). We can't find the only thing that can represent fruits (because apples and oranges can't represent fruits). It can be described by abstract classes, so abstract classes can't be instantiated. When we use a class to specifically describe "apple" This class can inherit the abstract class describing "fruit". We all know that "apple" is a kind of "fruit".

In the object-oriented domain, abstract classes are mainly used for type hiding. We can construct a fixed abstract description of a group of behaviors, but this group of behaviors can have any possible concrete implementation. This abstract description is an abstract class, and any set of possible concrete implementations are represented as all derived classes of this abstract class.

All abstract methods in interfaces and abstract classes cannot be implemented concretely, but all abstract methods should be implemented in their subclasses (there should be a function body, even if {} is empty). Java designers may consider the flexibility of abstract methods, and each subclass can implement abstract methods according to their own needs.

Abstract classes are defined as follows:

Interfaces are defined as follows:

In short, an abstract class is a class with incomplete functions. An interface is just a collection of abstract method declarations and static data that cannot be modified, neither of which can be instantiated.

In a sense, interface is a special form of abstract class. In Java language, abstract class represents an inheritance relationship. A class can only inherit one abstract class, but a class can implement multiple interfaces. In many cases, interfaces can indeed replace abstract classes if you don't need to deliberately express inheritance on properties.

I further understand that for the purpose of introducing abstract classes and interfaces into Java, the answers obtained from experts are as follows:

1. From the perspective of class hierarchy, abstract classes are at the top of the hierarchy, but in actual design, generally speaking, abstract classes should appear later. Why? In fact, the acquisition of abstract classes is a bit like the extraction of common factors in Mathematics: ax + BX, X is an abstract class. If you don't have the previous formula, how do you know whether x is a common factor? In this regard, it is also in line with the process of people's understanding of the world, first concrete and then abstract. Therefore, in the design process, if you get a large number of specific concepts and find their commonalities from them, it should be right that the collection of commonalities is an abstract class.

2. On the surface, interface is very similar to abstract classes, but its usage is completely different. Its basic function is to gather some irrelevant classes (Concepts) together to form a new and centralized "new class". A typical example I give students is "driver". Who can be a driver? Anyone can, as long as you get a driver's license. So I don't care whether you are a student, white-collar, blue-collar or boss, as long as you have a driver's license, you are a driver.

When I define the "car" class, I can specify the "driver".

At this time, the object of car doesn't care what the driver does. The only thing they have in common is that they have obtained a driver's license (both implement the driverlicense interface). This should be the most powerful part of the interface, which is unmatched by abstract classes.

Summary:

Abstract class is to extract the common factor of concrete class, and interface is to "hash" some irrelevant classes into a common group. Usually, we have a good habit of using multiple interfaces. After all, Java is single inheritance, unlike C + +, but we must use it when we need to use abstract classes (a little similar to goto usage). Ha ha.

I hope this article will be helpful to you in Java programming.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>