[introduction to Java] comparison of day3 abstract classes and interfaces

We've finished talking about abstract classes and interfaces. Now let's make a comparison.

In fact, to be honest, there is not much comparability. They are two completely different things, and their abstraction is not at the same level. However, in order to make everyone better understand, let's make a comparison. After all, they are very abstract (233).

The first is the grammatical comparison

1) abstract classes and interfaces cannot be instantiated because they are very virtual. However, there are some differences in access rights.

A. abstract methods in abstract classes (which are preceded by abstract modifier) cannot be modified with private, static, synchronized and native access modifiers. The reason is very simple. Let me talk about it slowly.

Abstract methods have no method body, and their purpose is to inherit. Therefore, if private modification is used, can't they be inherited? This violates its original design intention, so private cannot be used to modify abstract methods. As for static, the method modified with it can be called directly without instantiation, but the abstract method has no method body, so it is meaningless to use static modification. Synchronized is used to lock. If you modify a method in a class, it is equivalent to locking with this variable. However, abstract classes cannot be instantiated, and abstract methods are not implemented in this class, but in subclasses. Therefore, locks should belong to subclasses, so extraction methods cannot be modified with the synchronized keyword; As for native, this is in conflict with the abstract keyword itself. The abstract declaration method is given to the subclass implementation, while native is given to the local operating system implementation. If it occurs at the same time, it is equivalent to giving the implementation to the subclass and to the local operating system. Who will implement it in the end?

To sum up, abstract methods in abstract classes can only be modified with public and protected.

B. all methods in the interface are public abstract, and other modifiers cannot be used. By default (without any modifiers), they are also public abstract. Because the interface can only be implemented by the class and cannot be inherited by the class, protected modification cannot be used, but the interface can inherit the interface.

2) the only difference between an abstract class and an ordinary class is that it cannot be instantiated and can have abstract methods, so it can have constructors, static methods, static code blocks, and ordinary member variables and methods. However, the interface is different. The interface can only declare public abstract methods and public static final member variables.

3) an abstract class is essentially a class, which can only inherit from one class. A class can only inherit from one abstract class, but can implement multiple interfaces.

The second is the conceptual comparison

1) abstract classes are different from interfaces. Abstract classes generally abstract some classes with similar properties and methods to abstract a unified parent class. The interface is more an abstraction of a set of specific behaviors, focusing on behaviors, and the classes with these behaviors may not have much correlation.

For example, a plane can go to heaven and a bird can go to heaven. If you are more powerful, you should also go to heaven (escape), but obviously there is little correlation between the two. It seems unreasonable to insert a public parent for them. It looks like this:

Then define two classes to inherit it:

OK, now write a test class:

The operation results are as follows:

At first glance, it seems to work well, but when you think about it carefully, it seems inappropriate to forcibly insert two classes with low correlation into a parent class. After all, there is basically no similarity between aircraft and birds except that they can fly, and their flight mode, flight speed and altitude are very different, that is to say, in addition to this fly method, Other methods should be implemented in their own subclasses, and a class can only inherit one abstract class, so bird class and airplane class can no longer inherit other classes, which limits the flexibility of the program. Therefore, it is better to use the interface at this time:

At this time, you only need to change the extensions flyer of aircraft class and bird class to implementation flyable.

Modify the test class again:

The output is as follows:

Maybe we can't clearly see the difference between the two from this chestnut, so let's change another chestnut. People can take a plane, a train or a car, as long as they have manned function. The interface is realized as follows:

Define a passenger class to distinguish each passenger by name.

Define automobile class, train class and aircraft class respectively. They all implement icarrypassenger interface, and aircraft can also implement iflyable interface (although not used):

OK, now let's write a test class to test:

The output is as follows:

Because there is not much correlation between aircraft, trains and cars, it is obviously impossible to directly abstract the parent class. They only have the same behavior, that is, carrying passengers, so it is most appropriate to use the interface.

So far, the explanation of this article has been completed. Presumably, through this explanation, you should have a better understanding of the difference between abstract classes and interfaces. If you have better chestnuts, you are welcome to leave messages and continue to pay attention.

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
分享
二维码
< <上一篇
下一篇>>