A. Both are abstract classes and cannot be instantiated.
B. The interface implementation class and the subclass of the objective CT class must implement the declared abstract method.
2. Different
A. The interface must be implemented and implements must be used. abstract class must inherit and extends must be used.
B. A class can implement multiple interfaces, but a class can inherit only one abstract class.
C. Interface emphasizes the implementation of specific functions, while abstract class emphasizes the ownership relationship.
D. Although the interface implementation class and the class subclass must implement the corresponding abstract method, the implementation form is different. Every method in the interface is an abstract method and is declared only (Declaration, no method body). The implementation class must be implemented. Abstract class sub-classes can be selectively implemented. This option has two meanings:
First, not all methods in the abastract class are abstract. Only those methods with abstract information are abstract. sub-classes must be implemented. Methods without abstract must be defined in objective CT class.
Second, when the subclass of abstract class inherits from it, non-abstract methods can be directly inherited or overwritten. For abstract methods, you can choose to implement them, you can also declare its method as an abstract method, without implementation, and leave it to its subclass for implementation. However, this class must also be declared as an abstract class. It is an abstract class and cannot be instantiated.
E. abstract class is the intermediary between interface and class.
Interfaces are completely abstract and can only declare methods. They can only declare pulic methods, private and protected methods, method bodies, and instance variables. However, the interface can declare constant variables, and it is not difficult to find such an example in JDK. However, putting constant variables in an interface violates its purpose as an interface and obfuscated the different values of interfaces and classes. If necessary, you can put it in the corresponding abstract class or class.
Abstract class plays an important role in interface and class. Abstract class is abstract and can declare abstract methods to standardize the functions that must be implemented by subclass. On the other hand, abstract class can define the default method body for subclass to use or override directly. In addition, it can also define its own instance variables for subclass to use through inheritance.
3. Application scenarios of interfaces
A. the class and the class need to coordinate with specific interfaces, regardless of how they are implemented.
B. It can exist as an identifier that can implement specific functions, or it can be a pure identifier that does not exist in any interface method.
C. You need to regard a group of classes as a single class, and the caller only contacts this group of classes through interfaces.
D. a specific number of functions are required, and these functions may be completely unrelated.
4. Application scenarios of abstract class
In a word, you can use a unified interface, instance variables, or default methods. The most common ones are:
A. defines a set of interfaces, but does not want to force each implementation class to implement all interfaces. Abstract class can be used to define a set of method bodies, or even empty method bodies, and then the subclass will select the methods that interest it to override.
B. In some cases, the pure interface alone cannot satisfy the coordination between classes. In addition, the variables in the class that indicate the State must be used to distinguish different relations. Abstract's mediation can well meet this requirement.
C. standardizes a set of methods for mutual coordination. Some of these methods are common and independent of the State. They can be shared without sub-classes; in other ways, each subclass needs to implement specific functions based on its own specific State.
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/garyxie/archive/2008/03/10/2164270.aspx