The differences between abstract classes and interfaces are as follows:
① When classes inherit abstract classes, you only need to implement some specific methods and all abstract methods, while the interface must implement all the methods in it.
② There are no member variables in the interface, while the abstract class can have member variables.
The interface introduced in Java is mainly used to solve the problem of multi-inheritance.
1) the interface cannot contain non-abstract methods, but abstract classes can.
2) one class can implement multiple interfaces, but only one parent class can be used.
3) an interface does not belong to an inheritance structure and is not actually related to inheritance. Therefore, an unrelated class can implement the same interface.
Abstract classes and Methods
In all of our instruments, the methods in the basic instruments must be "pseudo" methods. If you call these methods, an error occurs. That is because instrument intends to create a common interface for all classes derived from it.
The only reason for establishing this universal interface is that it can make different representations for different subtypes. It creates a basic form for us to define "generic" in all the classes"
. To elaborate on this concept, another method is to call instrument "abstract base class" ("abstract class "). To process a series of classes through this universal interface, you need to create
Abstract classes. All the signature-class methods that match the signature of the base class declaration can be called through the dynamic binding mechanism (however, as stated in the previous section, if the method name is the same as the base class, but the independent variable
Or different parameters will lead to overload, which may not be what we want ).
If there is an abstract class like instrument, the object of that class is almost meaningless. In other words, the role of instrument is only to express interfaces, while
Not to express some specific implementation details. Therefore, creating an instrument object is meaningless, and we should usually prohibit users from doing that. For this purpose
All methods in the Instrument Display error messages. However, this will delay the information to the runtime and require thorough and reliable tests on the user side. In any case, the best way is to compile
Problems are caught during translation.
To address this problem, Java provides a mechanism named "abstract method ". It is an incomplete method that contains only one declaration and has no method subject. The following is the syntax used for abstract method declaration:
Abstract void X ();
A class that contains an abstract method is called an abstract class ". If a class contains one or more abstract methods, the class must be specified as abstract ). Otherwise, the compiler reports an error message to us.
If an abstract class is incomplete, what action will the compiler take once someone tries to generate an object of that class? Because it is not safe to create an object belonging to an abstract class, an error message is returned from the compiler. In this way, the compiler can ensure the "purity" of the abstract class, so we don't have to worry about misuse of it.
If you inherit from an abstract class and want to generate an object of a new type, you must provide method definitions for all abstract methods in the basic class. If you do not do this (you can choose not to do it), the category class will also be abstract, and the compiler will force us to use abstract keywords to mark the "abstract" nature of the class.
Even if no abstract method is included, a class can be declared as an "abstract class ". If a class does not need to have any abstract methods and we want to disable all instances of that class, this capability will be very useful.
Interface
The "interface" (Interface) keyword makes the abstract concept more in-depth. We can think of it as a "pure" abstract class. It allows the Creator to specify the basic form of a class: Method
Name, list of independent variables, and return type, but the method subject is not specified. The interface also contains data members of the basic data type, but both of them are static and final by default. The interface only provides one form, and
Implementation Details are not provided.
The interface describes itself as follows: "All the classes that implement me should look like me now ". Therefore, all Code We all know that this interface may call
The method used. This is the full meaning of the interface. Therefore, we often use interfaces to establish a "protocol" between classes ". Some object-oriented Program The design language uses a protocol"
(Protocol) keyword, which is the same as the interface.
To create an interface, use the interface keyword instead of the class. Similar to a class, we can add a public keyword before the interface keyword (but only the interface is defined in a file with the same name); or omit it, creates a "friendly" state.
To generate a class that matches a specific interface (or a group of interfaces), use the implements keyword. What we want to express is "the interface looks like that. Here is its specific work details ". All of our work is very similar to inheritance.
Http://hi.baidu.com/%B3%C2%C0%CB%CF%C9/blog/item/f8caaec3c21a3255b219a84c.html. It's more detailed here.