in theJavain language, abstract class andInterfaceis to supportAbstract classtwo types of mechanisms defined. It is the existence of these two mechanisms that gives Java a powerful object-oriented capability.
Understanding Abstract Classes
Abstract class and interface are used in the Java language to define abstractions, so what are abstract classes that can benefit us by using abstract classes?
In object-oriented concepts, we know that all objects are depicted through classes, but not in the opposite way. Not all classes are used to depict objects, and if a class does not contain enough information to depict a specific object, such a class is an abstract class. Abstract classes are often used to characterize the abstract concepts we derive from analysis and design of problem areas, and are abstractions of a series of concrete concepts that look different, but are essentially the same.
abstract classes are primarily used for type concealment in the object-oriented realm. We can construct an abstract description of a fixed set of behaviors, but this group of behaviors can have any possible concrete implementation. This abstract description is an abstract class, and any possible concrete implementation of this group is represented by all possible derived classes. The module can manipulate an abstract body. Because the module relies on a fixed abstraction, it can be modified, and the behavior of this module can be extended by deriving from this abstraction.
On the abstract class and interface from the perspective of grammar definition
at the syntactic level, the Java language gives different definitions for abstract class and interface, and the following is an example of defining an abstract class called Demo to illustrate this difference.
The way to define the demo abstract class using the abstract class is as follows:
Abstract class Demo{abstract void method1 (); abstract void method2 (); }
The way to define the demo abstract class using interface is as follows:
Interface demo{void method1 (); void Method2 (); ...}
In the abstract class mode, the demo can have its own data members, but also can have non-abstract member methods, and in the implementation of the interface method, the demo can only have static data members can not be modified (that is, must be static Final, but generally does not define data members in interface, all member methods are abstract. In a sense, interface is a special form of abstract class.
from a programmatic point of view, both the abstract class and the interface can be used to implement the idea of "design by contract". However, there are some differences in the specific use.
first, the abstract class represents an inheritance relationship in the Java language, and a class can only use one inheritance relationship (because Java does not support multiple inheritance----). However, a class can implement multiple interface. Perhaps this is a compromise of the Java language designer in considering Java's support for multiple inheritance.
Second, in the definition of abstract class, we can give the method the default behavior. However, in the definition of interface, the method does not have the default behavior, and in order to circumvent this restriction, the delegate must be used, but this adds some complexity and can sometimes cause a lot of trouble.
There is another serious problem with the inability to define default behavior in an abstract class, which can cause maintenance headaches. Because if you later want to modify the interface of the class (typically represented by an abstract class or interface) to accommodate the new situation (for example, adding new methods or adding new parameters to a used method), it can be very cumbersome and may take a lot of time (for many of the derived classes , in particular). However, if the interface is implemented by an abstract class, it is possible to modify the default behavior defined in the abstract class only.
Similarly, if the default behavior cannot be defined in an abstract class, it causes the same method implementation to appear in each of the derived classes of the abstract class, violating the "one Rule,one place" principle, resulting in code duplication, which is also detrimental to future maintenance. Therefore, you should be very careful when choosing between the abstract class and the interface.
abstract class and interface from the perspective of design concept
It mainly discusses the difference between abstract class and interface from the angle of grammar definition and programming, and the difference between these layers is comparatively low-level and non-essential.
as mentioned earlier, abstract class embodies an inheritance relationship in the Java language, in order to make the inheritance relationship reasonable, there must be a "is-a" relationship between the parent class and the derived class, that is, the parent class and the derived class should be the same in nature. For interface, it does not require that the interface and interface definitions be consistent in the concept, but only the contract that implements the interface definition. To make the discussion easy to understand, a simple example is described below.
Consider an example of the assumption that there is an abstraction about door in our problem area, that the door has the ability to execute two actions open and close, at which point we can pass an abstract Class or interface to define a type that represents the abstract concept:
define door using the abstract class method:
Abstract class Door{abstract void open (); abstract void Close ();}
Define door using the interface method:
Interface door{void open (); void close ();}
other specific door types can be extends using the door defined by the abstract class or implements interface defined using door mode. It seems that there is no big difference between using abstract class and interface.
If you now require door also have the function of alarm. How do we design the class structure for this example?
Solution:
simply add a alarm method to the definition of door, as follows:
Abstract class Door{abstract void open (); abstract void close (); abstract void alarm ();}
Or
Interface door{void open (); void close (); void alarm ();}
Then 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 approach violates a core principle in object-oriented design ISP (Interface segregation Principle), which mixes the behavior method inherent in door concept with another concept of "alarm" in the definition of door. One problem is that modules that rely solely on the concept of door are changed by the concept of "alarm" (e.g., modifying the parameters of the alarm method) and vice versa.
Summary
1.abstract class represents an inheritance relationship in the Java language, and a class can only use one inheritance relationship at a time. However, a class can implement multiple interface.
2. In abstract class, you can have your own data members, or you can have non-ABSTARCT member methods, and in interface, you can only have static data members that cannot be modified (that is, it must be static final, but Data members are not generally defined in interface, and all member methods are abstract.
The 3.abstract class and interface reflect a different design concept. In fact, abstract class represents the "is-a" relationship, interface represents the "like-a" relationship.
4. Classes that implement abstract classes and interfaces must implement all of these methods. Abstract classes can have non-abstract methods. There is no implementation method in the interface.
5. The variable defined in the interface is the public static final type by default, and must be given its initial value, so the implementation class cannot be redefined or changed.
6. Variables in abstract classes are friendly by default, whose values can be redefined in subclasses or re-assigned.
7. The methods in the interface are public,abstract type by default.
Conclusion
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 in fact a language of the customary method, I hope readers can understand the reader.
The difference between abstract class and interface of Black Horse programmer