Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the differences between abstract classes and interfaces in java

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you what are the differences between abstract classes and interfaces in java, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

In the Java language, abstract class and interface are two mechanisms that support abstract class definition. It is precisely because of the existence of these two mechanisms that Java is endowed with powerful object-oriented capabilities. There is a great similarity between abstract class and interface in terms of support for abstract class definitions, and they can even be replaced with each other, so many developers seem to choose abstract class and interface casually when entering abstract class definitions. In fact, there is still a big difference between the two, and their choice even reflects the understanding of the nature of the problem domain and whether the understanding of the design intention is correct and reasonable. This article will analyze the differences between them in an attempt to provide developers with a basis for choosing between them.

Understanding Abstract classes

Both abstract class and interface are used for abstract classes in the Java language (the abstract class in this article is not translated from abstract class, it represents an abstract body, and abstract class is a method used in the Java language to define abstract classes, please note the distinction), so what are abstract classes and what benefits can we get from using abstract classes?

In the object-oriented concept, we know that all objects are described by classes, but the reverse is not the case. Not all classes are used to describe objects. If a class does not contain enough information to describe a concrete object, such a class is an abstract class. Abstract class is often used to represent the abstract concepts that we get from the analysis and design of the problem domain, which is the abstraction of a series of concrete concepts that look different but are essentially the same.

For example, if we develop a graphic editing software, we will find that there are some specific concepts such as circles and triangles in the problem domain, which are different, but they all belong to the concept of shape. The concept of shape does not exist in the problem domain, it is an abstract concept. It is precisely because abstract concepts have no corresponding concrete concepts in the problem domain, so the abstract classes used to represent abstract concepts cannot be instantiated.

In the object-oriented field, abstract classes are mainly used for type hiding. We can construct an abstract description of a fixed set of behaviors, but this set of behaviors can be implemented in any possible way. This abstract description is the abstract class, and this set of possible concrete implementations represents all possible derived classes. The module can operate on an abstract body.

Because the module depends on a fixed abstraction, it can not be modified; at the same time, the behavioral function of the module can be extended by deriving from this abstraction. Readers familiar with OCP must know that abstract classes are the key to implementing one of the core principles of object-oriented design, OCP (Open-Closed Principle).

Abstract class and interface from the perspective of grammatical definition

At the syntax level, the Java language gives different definitions of abstract class and interface. Let's take defining an abstract class called Demo as an example to illustrate this difference.

The way to define Demo abstract classes using abstract class is as follows:

Abstract class Demo {abstract void method1 (); abstract void method2 (); … }

The way to define Demo abstract classes using interface is as follows:

Interface Demo {void method1 (); void method2 (); … }

In abstract class mode, Demo can have its own data members or non-abstract member methods, while in the implementation of interface mode, Demo can only have static data members that cannot be modified (that is, they must be static final, but data members are generally not defined in interface), and all member methods are abstract. In a sense, interface is a special form of abstract class.

From a programming perspective, both abstract class and interface can be used to implement the idea of "design by contract". But there are some differences in specific use.

First of all, abstract class represents an inheritance relationship in the Java language, and a class can only use an inheritance relationship once (because Java does not support multi-inheritance-transfer). However, a class can implement multiple interface. Perhaps this is a compromise made by the designers of the Java language when considering Java's support for multiple inheritance.

Second, in the definition of abstract class, we can assign the default behavior of the method. However, in the definition of interface, methods cannot have default behavior, and delegates must be used to bypass this limitation, but this can add some complexity and sometimes cause a lot of trouble.

Another serious problem with the inability to define default behavior in abstract classes is that it can cause maintenance problems. Because later, if you want to modify the interface of the class (usually expressed by abstract class or interface) to adapt to the new situation (for example, adding new methods or adding new parameters to the methods already used), it will be very troublesome and may take a lot of time (especially for many cases of derived classes). But if the interface is implemented through abstract class, you may only need to modify the default behavior defined in abstract class.

Similarly, if the default behavior cannot be defined in an abstract class, it will cause the same method implementation to appear in every derived class of the abstract class, violating the "one rule,one place" principle and causing code duplication, which is also not conducive to future maintenance. Therefore, be very careful when choosing between abstract class and interface.

Abstract class and interface from the perspective of design concept

The above mainly discusses the distinction between abstract class and interface from the perspective of syntax definition and programming, and the difference between these levels is relatively low-level and non-essential. This section will analyze the difference between the two from another level: the design concepts reflected by abstract class and interface. The author believes that only by analyzing from this level can we understand the essence of the two concepts.

As mentioned earlier, abstract class embodies an inheritance relationship in the Java language, and for the inheritance relationship to be reasonable, there must be a "is-a" relationship between the parent class and the derived class, that is, the parent class and the derived class should be conceptually the same. This is not the case for interface, which does not require that the implementer of interface and the definition of interface are conceptually consistent, but simply implement the contract of the definition of interface. In order to make the discussion easy to understand, the following will be illustrated by a simple example.

Consider such an example. Suppose there is an abstract concept of Door in our problem domain, and the Door has two actions, open and close. In this case, we can define a type that represents the abstract concept through abstract class or interface, as follows:

Use abstract class to define Door:

Abstract class Door {abstract void open (); abstract void close ();}

Use interface to define Door:

Interface Door {void open (); void close ();}

Other specific Door types can be Door defined by extends using abstract class or Door defined by implements using interface. It looks as if there is no big difference between using abstract class and interface.

If the Door is now required to have the function of alarm. How do we design the class structure for this example (in this case, it is mainly to show the difference between abstract class and interface in the design concept, and other irrelevant issues have been simplified or ignored)? The possible solutions are listed below, and these different solutions are analyzed from the design concept level.

Solution one:

Simply add an alarm method to the definition of Door as follows:

Abstract class Door {abstract void open (); abstract void close (); abstract void alarm ();}

Or

Interface Door {void open (); void close (); void alarm ();}

Then the definition of AlarmDoor with alarm function is as follows:

Class AlarmDoor extends Door {void open () {… } void close () {… } void alarm () {… }}

Or

Class AlarmDoor implements Door {void open () {… } void close () {… } void alarm () {… }}

This method violates ISP (Interface Segregation Principle), a core principle of object-oriented design, and mixes the behavior method inherent in the concept of Door with the behavior party method of another concept, "alarm", in the definition of Door. One problem that arises is that modules that rely solely on the concept of Door will change due to changes in the concept of "alarms" (such as changing the parameters of the alarm method), and vice versa.

Solution 2:

Since open, close, and alarm belong to two different concepts, they should be defined in abstract classes that represent these two concepts respectively according to the ISP principle. The definition methods are: both concepts are defined in abstract class mode; both concepts are defined in interface mode; one concept is defined in abstract class mode, and the other is defined in interface way.

Obviously, since the Java language does not support multiple inheritance, it is not feasible to define both concepts in the abstract class way. The latter two methods are feasible, but the choice of them reflects the understanding of the nature of the concept in the problem domain and whether the reflection of the design intention is correct and reasonable. Let's analyze and explain one by one.

If both concepts are defined in interface, it reflects two problems: 1. We may not understand the problem area clearly. Is AlarmDoor essentially a Door or an alarm? 2. If we have no problem with our understanding of the problem domain, for example, through the analysis of the problem domain, we find that the concept of AlarmDoor is essentially the same as that of Door, then we can not correctly reveal our design intention when we implement it, because the above meaning can not be reflected in the definition of these two concepts (both are defined by interface).

If our understanding of the problem domain is that AlarmDoor is essentially Door in concept, and it has the function of alarming. How should we design and implement to clearly reflect what we mean? As mentioned earlier, abstract class represents an inheritance relationship in the Java language, which is essentially a "is-a" relationship. So for the concept of Door, we should use abstarct class to define it. In addition, AlarmDoor also has an alarm function, which states that it can complete the behavior defined in the alarm concept, so the alarm concept can be defined by interface. As follows:

Abstract class Door {abstract void open (); abstract void close ();} interface Alarm {void alarm ();} class Alarm Door extends Door implements Alarm {void open () {… } void close () {… } void alarm () {… }}

This way of implementation can basically clearly reflect our understanding of the problem domain and correctly reveal our design intention. The real abstract class represents the "is-a" relationship, and the interface represents the "like-a" relationship, which can be used as a basis when choosing. Of course, this is based on the understanding of the problem domain. For example, if we think that AlarmDoor is essentially an alarm and has the function of Door, then the above definition will be reversed.

Summary

1.abstract class represents an inheritance relationship in the Java language, and a class can only use an inheritance relationship once. However, a class can implement multiple interface.

two。 In abstract class, you can have your own data members or non-abstarct member methods, while in interface, you can only have static data members that cannot be modified (that is, they must be static final, but data members are generally not defined in interface), and all member methods are abstract.

3.abstract class and interface reflect different design concepts. In fact, abstract class represents a "is-a" relationship, and interface represents a "like-a" relationship.

4. Classes that implement abstract classes and interfaces must implement all of their methods. There can be non-abstract methods in abstract classes. There can be no implementation methods in the interface.

5. The variable defined in the interface is public static final by default and must be given an initial value, so its value cannot be redefined or changed in the implementation class.

6. Variables in abstract classes are friendly by default, and their values can be redefined or reassigned in subclasses.

7. The methods in the interface are all of type public,abstract by default.

These are all the contents of the article "what are the differences between abstract classes and interfaces in java?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report