(1) Overview
Abstract class and interface support the definition of abstract classes in Java. It is precisely because of the existence of these two mechanisms
Only in this way can Java provide powerful object-oriented capabilities. Abstract class and interface support the definition of abstract classes
Therefore, many developers Define abstract classes and interfaces
The selection is relatively casual. In fact, there is a big difference between the two. Their choices even reflect the essence of the problem field.
Understanding of the design intent is correct and reasonable.
Abstract class and interface are used for abstract classes in Java language (the abstract classes in this article are not
It represents an abstract body, while abstract class is a method used to define abstract classes in Java.
Yes. So what is an abstract class? What are the benefits of using an abstract class?
In the concept of object-oriented, we know that all objects are depicted through classes, but this is not the case. Not all
Class is used to depict objects (class specific), if a class does not contain enough information to depict a specific object, such
Class is an abstract class. Abstract classes are often used to represent the abstract concepts we have come up with in the analysis and design of problem domains.
Different, but essentially identical abstract of specific concepts. For example, if we develop a graphic editing software, we will find the problem area.
There are some specific concepts such as circle and triangle, they are different, but they all belong to the concept of shape, the concept of shape in
The problem field does not exist. It is an abstract concept. It is precisely because the abstract concept does not have a specific concept in the problem field that is used
Abstract classes that characterize abstract concepts cannot be instantiated.
In the Object-Oriented field, abstract classes are mainly used to hide types. We can construct a fixed abstract description of a group of actions,
However, this group of actions can have any possible implementation method. This abstract description is an abstract class, and any possible implementations of this group
All possible Derived classes are displayed. For example, an animal is an abstract class. Humans, monkeys, and tigers are actually derived classes.
The animal type is used to hide human, monkey, and tiger types.
(2) analyze the syntax definition
At the syntax level, the Java language provides different definitions for abstract class and interface. The following describes how to define an abstract class named demo.
You can use abstract class to define a demo abstract class as follows:
Abstract class demo {
Abstract void Method1 ();
Abstract void method2 ();
...
}
The following method is used to define the demo abstract class using the interface:
Interface demo {
Void Method1 ();
Void method2 ();
...
}
In the abstract class method, the demo can have its own data members or non-abstract member methods. In the implementation of the interface method, the demo can only have
Static data members that cannot be modified (that is, they must be static final, but generally do not define data members in the interface). All member methods are abstract
. In a sense, interface is a special form of abstract class.
From the programming point of view, abstract class and interface can be used to implement the idea of "Design by contract. However, there are some differences in usage.
.
Abstract class represents an inheritance relationship in Java. A class can only use an inheritance relationship once (Because Java does not support multiple inheritance-reinjection ). However,
A class can implement multiple interfaces. Maybe this is a compromise between Java designers and Java's support for multi-inheritance.
Secondly, in the definition of abstract class, we can assign the default behavior of the method. However, in the interface definition, the method cannot have default behavior. to bypass this
Restrictions, must use the delegate, but this will increase some complexity, sometimes cause a lot of trouble.
Another serious problem still exists when the default behavior cannot be defined in the abstract class, which may cause maintenance trouble. Because if you want to modify the class interface (
Abstract class or interface representation) to adapt to new situations (for example, adding a new method or adding a new parameter to the used method), it will be very troublesome
, It may take a lot of time (especially when there are many derived classes ). However, if the interface is implemented through abstract class, you may only need to modify the definition.
The default behavior in abstract class can be used.
Similarly, if the default behavior cannot be defined in the abstract class, the same method will appear in every derived class of the abstract class, in violation of "one rule, one place"
Principle, resulting inCodeRepetition is also not conducive to future maintenance. Therefore, be careful when selecting abstract class and interface.
(3) Design Concepts
The preceding section mainly discusses the differences between abstract class and interface from the perspective of syntax definition and programming. The differences between these layers are relatively low-level and non-essential. This section starts
Level: abstract class and interface reflect the design concept to analyze the differences between the two. The author believes that only by analyzing at this level can we understand the essence of the two concepts.
.
As mentioned above, abstract class represents an inheritance relationship in Java. To make the inheritance relationship reasonable, there must be a "is-a" relationship between the parent class and the derived class,
That is, the concept of the parent class and the derived class should be essentially the same. For an interface, it is not required that the implementer of the interface and the interface definition are consistent in concept.
And implements the interface definition contract. In order to make the discussion easier to understand, we will explain it through a simple example below.
Consider this example. Suppose there is an abstract concept about the door in our problem field. The door has two open and close actions. Now we can use
Abstract class or interface is used to define a type that represents the abstract concept. The definitions are as follows:
Use abstract class to define door:
Abstract class door {
Abstract void open ();
Abstract void close ();
}
Use the interface method to define the door:
Interface door {
Void open ();
Void close ();
}
For other specific door types, extends can use the door defined in abstract class or implements to use the door defined in interface mode. It looks like you are using
Abstract class and interface have no major difference.
If you want the door to have the alarm function. How do we design the class structure for this example (in this example, we mainly want to demonstrate abstract class and
Interfaces are reflected in the differences in design concepts. Other irrelevant issues are simplified or ignored )? The following will list possible solutions and address these issues from the design concept layer.
Different solutions are analyzed.
Solution 1:
Add an alarm method to the door definition as follows:
Abstract class door {
Abstract void open ();
Abstract void close ();
Abstract void alarm ();
}
Or
Interface door {
Void open ();
Void close ();
Void alarm ();
}
The alarmdoor with alarm function is defined as follows:
Class alarmdoor extends door {
Void open (){...}
Void close (){...}
Void alarm (){...}
}
Or
Class alarmdoor implements door {
Void open (){...}
Void close (){...}
Void alarm (){...}
}
This method violates a core principle of ISP (interface segregation principle) in object-oriented design, and inherent behavior of the door concept in the definition of door
The method is mixed with the behavior method of another concept "alarm. One problem is that modules that only rely on the door concept will be caused by the concept of "alarm ".
Change (for example, modifying the parameters of the alarm method.
Solution 2:
Since open, close, and alarm belong to two different concepts, they should be defined in abstract classes that represent these two concepts according to the ISP principle. The two definitions are as follows:
Both concepts are defined in abstract class mode; both concepts are defined in interface mode; one is defined in abstract class mode, and the other is defined in abstract class mode.
Interface mode definition.
Obviously, because the Java language does not support multiple inheritance, both concepts are defined using abstract class. Both of the following methods are feasible,
Their selection reflects the understanding of the concept nature in the problem field, and whether the reflection of the design intent is correct and reasonable. Let's analyze and explain them one by one.
If both concepts are defined using the interface method, there are two problems: 1. We may not understand the problem field clearly. The concept of alarmdoor is essentially true.
Is it door or an alarm? 2. If we have no problem understanding about the problem field, for example, through analysis of the problem field, we find that alarmdoor is essentially a concept and door
As a result, our design intent cannot be correctly revealed during implementation, because the definition of these two concepts (both using the interface method definition) does not reflect the above
.
If our understanding of the problem field is: alarmdoor is essentially a door, it also has the alarm function. How should we design and implement it to clearly reflect
What do we mean? As mentioned above, abstract class represents an inheritance relation in Java, and the inheritance relation is essentially a "is-a" link. So the concept of door
, We should use the Abstarct class method to define. In addition, alarmdoor has the alarm function, indicating that it can complete the behaviors defined in the alarm concept, so the alarm concept can be
It is defined in interface mode. As follows:
Abstract class door {
Abstract void open ();
Abstract void close ();
}
Interface alarm {
Void alarm ();
}
Class alarm door extends door implements alarm {
Void open (){...}
Void close (){...}
Void alarm (){...}
}
This implementation method can clearly reflect our understanding of the problem field and correctly reveal our design intent. Abstract class indicates the "is-a" relation.
The interface indicates the "like-a" relationship, which can be used as a basis for your selection. Of course, this is based on your understanding of the problem field. For example:
The concept of alarmdoor is essentially an alarm, and it also has the function of door, so the above definition method will be reversed.
(4) abstract class Summary
1. abstract class usually contains one or more abstract methods, which are not implemented. Classes containing abstract methods must be declared as abstract classes. Abstract
All concrete subclasses of the class must provide specific implementation for the super class. If the subclass does not implement the abstract method of the super class, a compilation error will occur unless the subclass is declared as abstract.
2. abstract class declares the common attributes and behaviors of classes in the class hierarchy. Since constructors cannot be inherited, they cannot be declared as abstract methods.
Instantiate the object of an abstract class, but can declare the variable of the abstract type. This variable can be used to reference the object of the subclass.
(5) Summary of interfaces
1. The interface starts with interface and contains a set of abstract methods whose default value is public. The interface can contain variables. The default value is static final, and the initial value must be given.
The current class cannot be redefined or its value cannot be changed. The implementation interface must implement all the methods, the interface cannot have implementation methods, and all member methods are abstract.
2. If a class does not implement any interface method, it is an abstract class and must declare the class with the abstract keyword. Implementing an interface is like reaching an agreement with the compiler,
"I will declare all methods developed by this interface ".
(6) Summary
1. abstract class represents an inheritance relationship in Java. A class can only use an inheritance relationship once. However, a class can implement multiple interfaces.
2. In abstract class, you can have data members or non-Abstarct member methods. In interface, you can only have static data that cannot be modified.
All member methods are abstract.
3. abstract class and interface have different design concepts. Abstract class represents the "is-a" relation,
The interface represents the "like-a" relationship.
4. The interface is generally used to replace this class when there is no default implementation that can be inherited in the abstract class (that is, there is no instance variable or default method implementation.
5. abstract class is another form of contract, which is the design requirement for implementation, while the interface is the server's requirement for the client.
6. abstract class is a base class and cannot be instantiated. interfaces are declarations. each class of the corresponding interface must implement methods.
7. If a subclass implements an interface, all methods in the interface must be implemented (whether or not required); if it inherits an abstract class, you only need to implement the required methods
This is an advantage of an abstract class.
8. If the method name defined in an interface changes, all subclasses implementing this interface obviously cannot be compiled because the method names they implement do not exist. This is
This problem does not exist if a non-abstract method is modified in the abstract class, but a new method is added to the subclass.
9. Looking at the first two points, it seems that abstract classes have more advantages than interfaces, but they have an insurmountable disadvantage: that is, a subclass can only have one parent class. A extends B.
In this way, a has all the methods and functions of B, but when a still wants to have the functions of C. It cannot be implemented through a extends C, but it requires some detours. Current System Architecture
The trend is to break down functions as much as possible by targeting abstraction (excuses, abstract classes) rather than specific programming. This requires implementation of multiple interfaces. Obviously
Classes cannot provide such functions. From the perspective of system reconstruction, it is very convenient for a specific class to abstract the interface. You only need to write an interface that defines all the methods of a specific class,
Then you can use the implement interface for this specific class. Abstract classes are much more complex, such as B extends a and c extends B. If you want to abstract an abstract for C
Class D, you need to find its top-level A to start from scratch, because it cannot do C extends d
Bytes ------------------------------------------------------------------------------------
Java and C # are easy to use, but they are never understood in details. For example, I just found that abstract is also available in Java. For example, C # has the readonly field and has the const function.
From: http://hi.baidu.com/tsj2209/blog/item/99dd931a59d879dfad6e759f.html
Abstract class and interface support the definition of abstract classes in Java. Abstract class and interface have great similarity in support for abstract class definition, and can even be replaced with each other. In fact, there is a big difference between the two. Their choices even reflect the understanding of the nature of the problem domain, and whether the understanding of the design intent is correct and reasonable.
You cannot create an abstract class instance. However, you can create a variable whose type is an abstract class and point it to an instance of a specific subclass. Abstract constructors or abstract static methods are not allowed. The subclasses of abstract classes provide implementation for all abstract methods in their parent classes. Otherwise, they are also abstract classes.
An interface is a variant of an abstract class. All methods in the interface are abstract. Multi-inheritance can be achieved by implementing such an interface. None of the methods in the interface hasProgramBody. The interface can only define static final member variables. The implementation of an interface is similar to that of a subclass, except that the implementation class cannot inherit behaviors from the interface definition. When a class implements a special interface, it defines (to be given by the program body) all the methods of this interface. Then, it can call the interface method on any object that implements the interface class. Because there is an abstract class, it allows the interface name as the type of the referenced variable. Normally, dynamic Association editing will take effect. The reference can be converted to the interface type or from the interface type. The instanceof operator can be used to determine whether the class of an object implements the interface.
Interfaces can inherit interfaces. Abstract classes can be implemented (implements) interfaces. abstract classes can inherit object classes, provided that the object classes must have clear constructors. The interface is more concerned with "what functions can be implemented", regardless of "how to implement ".
1. Similarities
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. Differences
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. Consider a group of classes as a single
Java and C # are easy to use, but they are never understood in details. For example, I just found that abstract is also available in Java. For example, C # has the readonly field and has the const function.
From: http://hi.baidu.com/tsj2209/blog/item/99dd931a59d879dfad6e759f.html
Abstract class and interface support the definition of abstract classes in Java. Abstract class and interface have great similarity in support for abstract class definition, and can even be replaced with each other. In fact, there is a big difference between the two. Their choices even reflect the understanding of the nature of the problem domain, and whether the understanding of the design intent is correct and reasonable.
You cannot create an abstract class instance. However, you can create a variable whose type is an abstract class and point it to an instance of a specific subclass. Abstract constructors or abstract static methods are not allowed. The subclasses of abstract classes provide implementation for all abstract methods in their parent classes. Otherwise, they are also abstract classes.
An interface is a variant of an abstract class. All methods in the interface are abstract. Multi-inheritance can be achieved by implementing such an interface. None of the methods in the interface has a program body. The interface can only define static final member variables. The implementation of an interface is similar to that of a subclass, except that the implementation class cannot inherit behaviors from the interface definition. When a class implements a special interface, it defines (to be given by the program body) all the methods of this interface. Then, it can call the interface method on any object that implements the interface class. Because there is an abstract class, it allows the interface name as the type of the referenced variable. Normally, dynamic Association editing will take effect. The reference can be converted to the interface type or from the interface type. The instanceof operator can be used to determine whether the class of an object implements the interface.
Interfaces can inherit interfaces. Abstract classes can be implemented (implements) interfaces. abstract classes can inherit object classes, provided that the object classes must have clear constructors. The interface is more concerned with "what functions can be implemented", regardless of "how to implement ".
1. Similarities
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. Differences
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. Consider a group of classes as a single