Simple comparison of the similarities and differences in order to clarify.
The difference between the concept of grammar and practical application
Abstract class:
The class of the abstract modifier is an abstraction class, and the abstract class cannot create an entity object.
Methods in abstract classes do not have to be abstract, and abstract methods defined in abstract classes must be implemented by subclasses, that is, overrides.
Therefore, there can be no abstract construction method or abstract static method. If the subclass does not implement a method that abstracts the parent class,
Then, subclasses must also be defined as abstract classes.
Interface:
The interface can be said to some extent as a special case of abstract class.
All methods in an interface must be abstract, and the method definition in the interface defaults to public abstract.
The variables in the interface are global variables, that is, public static final decoration.
In brief, the grammatical differences are:
1. The abstract class can have a construction method, and the interface cannot have a construction method.
2. Abstract classes can have ordinary member variables, and interfaces cannot have ordinary member variables.
3. Abstract classes can contain non-abstract ordinary methods, and all methods in an interface must be abstract.
4. The access type of an abstract method in an abstract class can be public,protected and the default type, but the abstract method in the interface
Can only be of type public, and the default is public abstract type.
5. There can be static methods in an abstract class, but not in an interface.
6. Abstract classes and interfaces can contain static member variables, the access type of static member variables in an abstract class can be arbitrary,
However, the static member variables in the interface can only be public static, and the default is public static.
7. A class can implement multiple interfaces, but can inherit only one abstract class.
The difference between the two applications:
Interface is more in the system framework design method to play a role in the main definition of communication between modules, and abstract classes in the implementation of code to play a role in the implementation of code reuse.
Second, from the example of simple experience the difference between the two
1/** Gate Class */2 abstract class Door{3 4 abstract Void Open () {};5 6 abstract void Close () {};7}
1/** an Alarm interface */2 interface alarm{3 4 void Alarm (); 5 6}
1/** for doors with alarm function */2 class Alarmdoor extends Door implements alarm{3 4 void Open () {...} 5 6 void Close () {...} 7
8 void Alarm () {...} 9 10}
The above simple example can intuitively feel the use of abstract classes and interfaces, and the implementation of this method can clearly reflect the correct understanding of the problem.
In fact, abstract class represents "is a", interface represents "like a" relationship, you can choose as a basis, of course, this is based on the understanding of the problem areas, For example: If we think that alarmdoor in the concept is essentially an alarm, but also has the function of door, then the definition of the above method will be reversed.
Abstract class and interface are two of the ways in which abstractions are defined in the Java language, and there is a great similarity between them. However, their choices often reflect an understanding of the nature of the concepts in the problem domain, the correctness and reasonableness of the design intent, as they represent the different relationships between concepts (though they are capable of fulfilling the function of demand). This is actually a kind of language of the customary law.
To summarize a few words:
1. Abstract classes and interfaces cannot be instantiated directly, and if instantiated, the abstract class variable must point to the subclass object that implements all the abstract methods, and the interface variable must point to the class object that implements all the interface methods.
2, abstract class to Quilt class inheritance, interface to be class implementation.
3, interface can only do method declaration, abstract class can do method declaration, can also do method to achieve
4, the variables defined in the interface can only be public static constants, the variables in the abstract class are ordinary variables.
5. Abstract methods in abstract classes must all be implemented by the quilt class, if the subclass can not fully implement the parent class abstract method, then the subclass can only be abstract class. Similarly, when an interface is implemented, if the interface method cannot be fully implemented, then the class can only be an abstract class.
6, the abstract method can only affirm, cannot realize. abstract void ABC (); cannot be written as abstract void abc () {}.
7, abstract class can have no abstract method
8, if there is an abstract method in a class, then this class can only be abstract class
9, abstract methods to be implemented, so can not be static, nor can it be private.
10, the interface can inherit the interface, and can inherit the interface more, but the class can only inherit one root.
In particular, for the common implementation code, the abstract class has its advantages. Abstract classes can guarantee the hierarchical relationship of implementation and avoid duplication of code. However, even when using abstract classes, it is important not to overlook the principle of defining behavior models through interfaces. From a practical point of view, if you rely on abstract classes to define behavior, it often leads to overly complex inheritance relationships, and it is more efficient to separate behaviors and implementations through interface definition behavior, which facilitates the maintenance and modification of code.
Distinguish between abstract classes and interfaces