| |

VerySource

 Forgot password?
 Register
Search
Author: hewei0611

Hello everyone, I want to ask a confusion about the interface

[Copy link]

0

Threads

9

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

Post time: 2020-12-19 20:15:01
| Show all posts
The way to define the Demo abstract class using abstract class is as follows:

abstract class Demo {
abstract void method1();
abstract void method2();



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

interface Demo {
void method1();
void method2();

}

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

For the more detailed issues of abstract class and interface at the grammatical definition level, it is not the focus of this article, so I will not repeat them. Readers can refer to Reference [1] for more relevant content.


Look at abstract class and interface from the programming level

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

First of all, abstract class represents an inheritance relationship in the Java language, and a class can only use the inheritance relationship once. However, a class can implement multiple interfaces. Perhaps this is a compromise considered by the designers of the Java language in considering Java's support for multiple inheritance.

Secondly, in the definition of abstract class, we can give the default behavior of the method. However, in the definition of interface, methods cannot have default behavior. In order to circumvent this limitation, delegation must be used, but this will add some complexity and sometimes cause great trouble.

There is another serious problem in the inability to define the default behavior in the abstract class, that is, it may cause maintenance troubles. Because if you later want to modify the interface of the class (usually represented by abstract class or interface) to adapt to new situations (for example, adding new methods or adding new parameters to existing methods), it will be very troublesome. It may take a lot of time (especially if there are many derived classes). But if the interface is implemented by an abstract class, you may only need to modify the default behavior defined in the abstract class.

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


Look at abstract class and interface from the design concept level

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

Use magic Report

0

Threads

5

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-12-20 07:30:01
| Show all posts
If there are not only people, but also animals, or aliens. .
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list