Interfaces and internal classes provide us with a more structured method to separate interfaces from implementations.
Abstract classes and interfaces are two types of mechanisms used to define abstract concepts in Java. They give Java powerful object-oriented capabilities. Their support for abstract concepts is very similar, or even interchangeable, but there are also differences between them.
I. abstract classes
We all know that everything in the Object-Oriented field is an object, and all objects are described through classes, but not all classes describe objects. If a class does not have enough information to describe a specific object and other specific classes are required to support it, such classes are called abstract classes. For example, new animal (), we all know that this is an animal object, but we do not know what this animal looks like. It does not have the concept of a specific animal, so he is an abstract class. We need a specific animal, such as a dog or a cat, to describe it in a specific way.
Abstract concepts in the Object-Oriented field do not have specific concepts in the problem field. Therefore, abstract classes used to characterize abstract concepts cannot be instantiated.
Abstract classes also reflect the idea of data abstraction and are a mechanism for realizing polymorphism. It defines a set of abstract methods. As for the specific manifestation of this set of abstract methods, it is implemented by a derived class. At the same time, the abstract class provides the concept of inheritance. Its starting point is to inherit, otherwise it has no meaning. Therefore, the defined abstract class must be used for inheritance. At the same time, in a hierarchy of inheritance links with abstract classes as nodes, leaf nodes must be specific implementation classes. (I do not know if this is correct !!! Expert guidance ....)
When using abstract classes, pay attention to the following points:
1. the abstract class cannot be instantiated. The instantiation should be completed by its subclass. It only needs a reference.
2. abstract methods must be overwritten by subclasses.
3. As long as the abstract class contains an abstract method, this method must be defined as an abstract class, whether or not it contains other methods.
4. An abstract class can contain specific methods. Of course, it can not contain abstract methods.
5. the abstract method in the subclass cannot be the same as the abstract method of the parent class.
6. Abstract cannot be used in parallel with final to modify the same class.
7. Abstract cannot be used in combination with private, static, final, or native to modify the same method. ,
Instance:
Define an abstract animal and provide an abstract method called cry (). Cats and dogs are subclasses of animals. Since cry () is an abstract method, cat and dog must implement cry () method. As follows:
Public abstract class animal {public abstract void cry ();} public class cat extends animal {@ override public void cry () {system. out. println ("cat name: Meow... ") ;}} public class dog extends animal {@ override public void cry () {system. out. println ("dog name: Wang... ") ;}} public class test {public static void main (string [] ARGs) {animal a1 = new CAT (); animal a2 = new dog (); a1.cry (); a2.cry () ;}------------------------------------------------------------------------ output: maomeow... dog name: Wang...
It is very useful to create abstract classes and abstract methods because they can clarify the abstraction of classes and tell users and compilers how to use them. abstract classes are useful reconstructor, because they allow us to easily move public methods up along the inheritance hierarchy. (From: Think in Java)
Ii. Interfaces
An interface is a more abstract "class" than an abstract class ". I can't find a better word to represent the "class", but we need to make it clear that the interface itself is not a class. We can see from the fact that we cannot instantiate an interface. For example, new runnable (); is definitely incorrect. We can only use the new implementation class.
An interface is used to establish a protocol between classes. It provides only one form without specific implementation. At the same time, the implementation class that implements this interface must implement all the methods of this interface. By using the implements keyword, it indicates that the class is following a specific or group of interfaces, it also indicates that "interface is just its appearance, but now we need to declare how it works ".
An interface is an extension of an abstract class. Java ensures that data security cannot be inherited in multiple ways. That is to say, only one parent class can be inherited, but the interfaces are different. One class can implement multiple interfaces at the same time, regardless of the relationship between these interfaces, the interfaces make up for the defect that abstract classes cannot inherit multiple types. However, it is recommended that inheritance and interfaces be used together, because this ensures data security and achieves multi-inheritance.
Pay attention to the following issues when using interfaces:
1. All access permissions of interfaces are automatically declared as public. Specifically, it can only be public. Of course, you can display the declaration as protected and private, but the compilation will fail!
2. The interface can define "member variables" or immutable constants, because the "member variables" in the interface will automatically become public static final. You can directly access implementclass. Name using the class name.
3. There is no implementation method in the interface.
4. All methods of an interface must be implemented in non-abstract classes. Abstract classes do not need to be implemented.
5. You cannot use the new operator to instantiate an interface, but you can declare an interface variable. This variable must be referenced (refer to) an object of the class that implements this interface. You can use instanceof to check whether an object has implemented a specific interface. For example, if (anobject instanceof comparable ){}.
6. Avoid repeated method names when implementing multiple interfaces.
Iii. Differences between abstract classes and interfaces
Although abstract classes and interfaces have large similarities, and sometimes they can be exchanged, this does not make up for the differences between them. Abstract classes and interfaces are described in the following two aspects: syntax level and design level.
3.1 syntax level
At the syntax level, Java provides different definitions for abstract classes and interfaces. The following demo classes are used to illustrate the differences between them.
Use abstract classes to implement:
Public abstract class demo {abstract void Method1 (); void method2 () {// Implementation }}
Using Interfaces
interface Demo { void method1(); void method2();}
In the abstract class method, the abstract class can have member data of any range, and can also have its own non-abstract method, but in the interface method, it can only have static and unchangeable member data (but we generally do not use Member Data in interfaces), and all its methods must be abstract. To some extent, interfaces are the specificity of abstract classes.
For a subclass, it can inherit only one abstract class (this is Java's consideration for data security), but it can implement multiple interfaces.
3.2 Design Level
The above only distinguishes the relationship between them from the perspective of syntax and programming. These are all low-level. To really use abstract classes and interfaces, we must distinguish them from a higher level. Only from the perspective of design concepts can we see their essence. Generally, they have the following three differences:
1. Different abstraction levels. Abstract classes are abstract classes, while interfaces are abstract actions. Abstract classes abstract the entire class, including attributes and behaviors, but interfaces abstract the local (behavior) of the class.
2. The cross-domain is different. An abstract class is a class with similar characteristics across domains, but an interface can have different classes across domains. We know that the abstract class discovers the common part from the subclass and then extends it to the abstract class. The subclass inherits the parent class, but the interfaces are different. Sub-classes that implement it can have no relationships and commonalities. For example, a cat or a dog can be abstracted into an abstract animal class and can be called as a method. Birds and planes can implement fly-fly interfaces and are capable of flying. Here we cannot share birds and planes with one parent class! Abstract classes reflect an inheritance relationship. 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, the implementer of the interface is not required to be consistent with the interface definition in concept. It only implements the contract of the interface definition.
3. Different design layers. For an abstract class, it is designed from the bottom up. We need to first know that the subclass can abstract the parent class, while the interface is different, and it does not need to know the existence of the subclass, you only need to define a rule. I do not know what sub-classes and how to implement them. For example, we only have one cat class here. If you abstract it into an animal class, isn't it too much design? We should have at least two animal classes, where cats and dogs are here. Let's abstract what they have in common to form an abstract animal class! Therefore, abstract classes are often reconstructed! But the interface is different. For example, we don't know how to implement the flying interface. What we need to do is to define the Flying Behavior interface beforehand. Therefore, abstract classes are abstracted from the bottom up, and interfaces are designed from the top down.
(The above are purely personal opinions. If there are any discrepancies or errors, please kindly advise !!!!)
To better illustrate the differences between them, we will use an example below. This example is derived from: http://blog.csdn.net/ttgjz/article/details/2960451
We have an abstract concept of door, which has two behaviors: open () and close (). At this time, we can define this abstract concept through abstract classes and interfaces:
Abstract class:
abstract class Door{ abstract void open(); abstract void close();}
Interface
interface Door{ void open(); void close();}
For other specific classes, you can use the abstract class to define the door or implements using the interface to define the door. Here we find that there is no big difference between the two.
But how can we achieve this if we need an alarm function?
Solution 1: Add an Alarm Method to the door: clarm ();
abstract class Door{ abstract void open(); abstract void close(); abstract void alarm();}
Or
interface Door{ void open(); void close(); void alarm();}
This method violates a core principle in object-oriented design. ISP (interface segregation principle)-See annotations, in the definition of the door, the inherent behavior method of the door concept is mixed with the behavior method of another concept "alarm. One problem is that the modules that rely solely on the door concept will change because of the change in the concept of "alarm", and vice versa.
Solution 2
Since open (), close (), and alarm () belong to two different concepts, we define them separately in two abstract classes that represent two different concepts based on the ISP principle, three methods are defined:
1. Both are defined using abstract classes.
2. Both are defined using interfaces.
3. Define an abstract class and an interface.
Because Java does not support multi-inheritance, the first one is not feasible. The latter two are feasible, but what you choose reflects your understanding of the nature of the problem domain.
If the second method is used to define the interface, two problems are reflected: 1. We may not understand the problem domain clearly. The concept of alarmdoor is actually a door-to-door alarm. 2. If we have no problem understanding about the problem domain, for example, we confirm that alarmdoor is essentially consistent in terms of concept during analysis, therefore, our design intent is not correctly reflected during the design. Because you use two interfaces for definition, their definition does not reflect the above meaning.
Third, if we understand the problem domain like this: alarmdoor is essentially door, but it also has the alarm behavior function, at this time, we can use the third solution to illustrate our design intent. The essence of alarmdoor is people. Therefore, we use abstract classes to define this concept. At the same time, alarmdoor has the alarm function, indicating that it can complete the behavior function defined in the alarm concept, therefore, alarm can be defined using interfaces. As follows:
abstract class Door{ abstract void open(); abstract void close();}interface Alarm{ void alarm();}class AlarmDoor 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. In fact, the abstract class represents the "is-a" relation, and the interface represents the "like-a" relation. You can use this as a basis for your selection, of course, this is based on the understanding of the problem field. For example, if we think that alarmdoor is essentially an alarm and has the door function, then the above definition method will be reversed.
Annotation:
ISP (interface segregation principle): a core object-oriented principle. It indicates that it is better to use multiple special interfaces than to use a single total interface.
The dependence of a class on another class should be based on the smallest interface.
An interface represents a role and should not assign different roles to an interface. Unrelated interfaces are merged to form a bloated large interface, which is a pollution to roles and interfaces.
Iv. Summary
1. An abstract class represents an inheritance relationship in Java. A subclass can have only one parent class, but multiple interfaces.
2. the abstract class can have its own member variables and non-abstract class methods, but the interface can only have static immutable member data (but generally does not define Member Data in the interface ), and all its methods are abstract.
3. abstract classes and interfaces reflect different design concepts. abstract classes represent the "is-a" relationship, the interface represents the relationship of "like-.
Abstract classes and interfaces are two different abstract concepts in the Java language. Their existence provides excellent support for polymorphism, although they have great similarity. However, their choices often reflect your understanding of the problem domain. Only by having a good understanding of the nature of the problem domain can we make a correct and reasonable design.
Consolidate the foundation, improve technology, and avoid difficulties. Climb the peak !!!!!!