The Similarities and Differences between Interface and Abstract Class

xiaoxiao2021-03-06  44

1. The same and contact

A. Both are abstract classes, and they cannot be instantiated.

B. The subclass of the INTERFACE implementation class and the Abstrct Class must implement the abstract method that has declared.

2. Different

A. Interface needs to be implemented, to use Implements, and Abstract Class needs to inherit, with Extends.

B. A class can implement multiple interface, but a class can only inherit an Abstract Class.

C. Interface emphasizes the implementation of a particular function, and Abstract Class emphasizes the relationship.

D. Although the subclass of the Interface implementation class and the AbstrCT Class must implement the corresponding abstract method, the implementation is different. Each method in Interface is an abstract method, which is just a declaration, no method body, and the implementation class must be implemented. The subclasses of the Abstract Class can be selected. This choice has two meanings:

First, there is not all methods in the Abastract Class, which is abstract, only those who have Abstract to abstract is abstract, and the subclasses must be realized. Those methods that do not have Abstract, must define the method body in the AbStrct Class.

Second, the subclass of Abstract Class In inherits it, the non-abstract method can be inherited directly or overwritten; and the abstract method can choose to achieve, or by re-declaring its method is an abstract manner, there is no need to implement, stay To achieve a subclass, such must also be declared as an abstract class. It is both an abstract class and of course not instantiated.

E. Abstract Class is the intermediary of Interface and Class.

Interface is completely abstract, can only declare the method, and can only declare the method of PULIC, and cannot declare the private and protected methods, and cannot define the method, and the instance variable cannot be declared. However, Interface can declare a constant variable and it is not difficult to find this example in JDK. However, the constant variables are placed in Interface in Interface to violate their role as an interface, but also confused the different value of Interface and class. If it is true, it can be placed in the corresponding Abstract Class or Class.

The Abstract Class played a role in Interface and Class. On the one hand, Abstract Class is abstract, which can declare the abstraction method to specify the functionality that the subclass must implement; on the other hand, it can define the default method body, and the ingredients are directly used or covered. In addition, it can define their own instance variables to use the bouncing by inheritance.

3. Interface application

A. Classs require a specific interface to coordinate without the class, instead of how it is implemented.

B. As an identification that can realize a particular function, it can also be a purely identifier that is not in the interface method.

C. Need to treat a set of classes as a single class, and the caller is connected to this group with this group.

D. You need to implement a specific number of features, and these functions may not have any contact.

4. Appract Application Applications for Abstract Class

In a word, it can be used in the case where the unified interface is required, and it needs an instance variable or the default method. The most common:

A. Define a set of interfaces, but do not want to force each implementation class to implement all interfaces. A group of method bodies can be defined with Abstract Class, or even a null method, and then the subclass is selected to cover the method you are interested in.

B. In some cases, only the pure interface cannot meet the coordination between classes and classes, and the variables indicated in the class are required to distinguish between different relationships. The mediation role of Abstract can meet this. C. Specify a group of mutual coordinated methods, some of which are common, and the status is independent, can be shared, no needed classes, but other methods require each subclass according to their own specific status to implement specific status Function.

转载请注明原文地址:https://www.9cbs.com/read-57098.html

New Post(0)