Abstract class and interface are two mechanisms that support the definition of abstract classes in the Java language, and it is because of the existence of these two mechanisms that give Java a powerful object-oriented capability, and there are similarities between them but there are many differences in actual usage.
1. Abstract class
In the object-oriented concept, all objects are described by classes, but conversely, 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.
For example: If we do a graphic editing software development, we will find that there are problems in the field of circles, triangles and some specific concepts, they are different, but they all belong to the shape of a concept, the concept of shape in the problem area does not exist, it is an abstract concept. It is because abstract concepts do not have specific concepts in the domain of the problem, so abstract classes that represent abstract concepts cannot be instantiated.
Abstract classes are defined as: classes that contain abstract methods;
Public abstract class ClassName {
abstract void fun ();
}
abstract methods: only declarations, and no specific implementations, abstract method format:
abstract void fun ();
Abstract methods must be decorated with the abstract keyword. If a class contains an abstract method, the class is called an abstract class, and the abstract class must be decorated with the abstract keyword before the class. Because an abstract class contains methods that do not have a specific implementation, you cannot create an object with an abstract class.
A class that contains abstract methods is called an abstract class, but does not imply that there can be only abstract methods in an abstract class, which, like ordinary classes, can also have member variables and ordinary member methods. Note that there are three main differences between an abstract class and an ordinary class:
1 The abstract method must be public or protected (because if private, you cannot inherit the quilt class, the subclass will not implement the method), by default, public.
2 Abstract classes cannot be used to create objects;
3 If a class inherits from an abstract class, the subclass must implement the abstract method of the parent class. If the subclass does not have an abstract method that implements the parent class, the subclass must also be defined as an abstract class.
In other ways, there is no difference between an abstract class and an ordinary class.
2. Interface 2.1 Definition related
An interface says: "All classes that implement this particular interface look like this." So any code that uses a particular interface knows which methods of the interface can be invoked and only needs to know, and the interface is used to establish the protocol between the class and the class. But interface is not just an extremely abstract class, because it allows people to implement features similar to multiple inheritance variants by creating a type that can be transformed upwards into multiple base classes.
[Public] interface InterfaceName {
}
The interface can contain variables and methods. Note, however, that the variables in the interface are implicitly specified as public static final variables (and only public static final variables, which are reported to compile errors with private adornments), and methods are implicitly specified as public The abstract method can only be public abstract (with other keywords, such as private, protected, static, final, etc.), and all the methods in the interface cannot have a specific implementation, that is to say, The methods in the interface must all be abstract methods. From here you can see the difference between the interface and the abstract class, the interface is an extremely abstract type, it is more "abstract" than the abstract class, and generally does not define variables in the interface.
To have a class follow a specific set of interfaces, you need to use the Implements keyword, which is as follows:
Class ClassName implements interface1,interface2,[...] {
}
As an interface, a class can inherit from the interface (or interface), this is also more inheritance, the interface of the member variables are not specifically belong to an object, are static member variables, belong to the entire class, so a class to implement a number of interfaces is also indifferent, there will be no conflict between the objects of the problem. Implementing multiple interfaces also enables multiple inheritance, and avoids problems with multiple inheritance, which is the benefit of implementing multiple inheritance with interfaces.
2.2 Significance of the separation interface
We used interface, but this interface doesn't reduce the amount of work we do when we define classes. We still have to write the class exactly as before. We should even be more careful not to violate the rules of interface. That being the case, why do we use interface?
In fact, interface is like an industry standard. A factory (class) Can adopt industry standards (Implement Interface), or it may not adopt industry standards. However, a product that incorporates industry standards will have the following benefits: Higher quality: The glass without water function does not conform to the standard. Easier to promote: Just like the USB interface on a computer, downstream products can be more easily connected.
If we already have a Java program that handles objects that match the Cup interface, such as getting the kids to drink water. So, as long as we are sure that we have a cup interface for the children's cups, we can ensure that the child can perform the action of drinking water. As for how the Cup (object) specifically defines the action of drinking water, we can leave it to the appropriate class to decide (for example, to drink water with a straw, or to open a small mouth to drink water).
In computer science, interfaces are a very important concept. For example, any operating system that provides a UNIX interface can be called a UNIX system. Linux,mac Os,solaris are UNIX systems that provide similar interfaces. However, the specific implementation of each system (source code) is different. Linux is open source, you can view every line of code, but you still don't know how to write a Solaris system. 2.3 Examples in the solution
I know comparable this interface is used to compare two objects, so how to compare it.
Numbers have a comparison method of numbers, strings have a comparison method of strings, students (their defined classes) also have their own comparison methods. Then, in the other code that is responsible for sorting the object (not necessarily the number), you definitely need to compare two objects.
What type are these two objects? Object a,b. Certainly not, a > b such a syntax cannot be compiled. int a,b. I can't do that. I said it from the beginning, not necessarily the numbers.
So, comparable is coming. He tells the compiler that a B two objects all satisfy the comparable interface, that is, they can be compared. Specifically how to compare, this procedure does not need to know. So, he needs some specific implementation, comparable interface has a method, called CompareTo. Then this method is used to replace the <, > such operators. Because operators are used by the compiler to preserve the built-in type (integers, floating-point numbers) rather than a generalized comparison operation.
If you can understand that the JDK has interfaces like comparable in its own library, it's easy to understand why you need interfaces when developing programs.
Programming is oriented toward abstraction (interface) programming. The benefits of doing so are many, in the case of the most votes are also reflected, that is, procedures can be expanded. When you modify the class of your specific implementation (or add a new implementation Class), you do not need to modify the class that invokes it; this is the "open for expansion, close to modification" principle that is emphasized in Java programming. In design mode, many of them need to use the interface. Of course, this is also the embodiment of Java polymorphism. The second is that Java implements multiple inheritance through interface. One of the benefits of inheritance is the reuse of code and the reduction of code errors. In this, the interface is equivalent to a specification, that is, to implement this interface, you must implement the interface method, complete the corresponding functions. If there is no interface, then in the implementation, it is likely that the method, or the definition of the method is not uniform.
3. Interface and abstract class differences 3.1 Grammatical level
1 abstract class can provide the implementation details of the member method, while the interface can only exist public abstract method;
2 The member variables in the abstract class can be of various types, while the member variables in the interface can only be public static final types;
3 The interface cannot contain static code blocks and static methods, while abstract classes can have static code blocks and static methods;
4 A class can inherit only one abstract class, while a class may implement multiple interfaces.
3.2 Design Level
1 abstract class is an abstraction of a thing, that is, a class abstraction, and an interface is an abstraction of the behavior. An abstract class is an abstraction of the whole class as a whole, including attributes, behavior, but an interface that abstracts the local (behavior) of the class. For a simple example, airplanes and birds are different kinds of things, but they all have one thing in common, they all fly. Then in the design, the aircraft can be designed as a class airplane, the birds are designed as a class bird, but can not fly this feature is also designed as a class, so it is only a behavioral characteristics, not the abstract description of a class of things. The flight can then be designed as an interface fly, containing the method fly (), and then airplane and bird implement fly this interface separately according to their own needs. Then as for different kinds of aircraft, such as fighter jets, civil aircraft, such as direct inheritance airplane can, for birds are similar, different kinds of birds directly inherit bird class can. As can be seen from here, inheritance is a "is not" relationship, and interface implementation is "there is no" relationship. If a class inherits an abstract class, then the subclass must be the kind of abstract class, and the interface implementation is there is no, have no relationship, such as whether the bird can fly (or whether it has the characteristics of flight), can fly can implement this interface, can not fly do not implement this interface.
2 The design level is different, abstract class as many subclasses of the parent class, it is a template design. And the interface is a code of conduct, it is a kind of radiant design. What is template design. The simplest example, we have used the template in ppt, if using template A to design ppt B and ppt c,ppt B and ppt C public part is the template A, if their public part needs to change, then only need to change template A on it, do not need to change PPT B and ppt c. And the radiant design, such as a certain elevator installed some kind of alarm, once you want to update the alarm, you must update all. That is to say, for an abstract class, if you need to add a new method, you can add a specific implementation directly to the abstract class, and the subclass can not make changes, but not for the interface, and if the interface is changed, all classes that implement this interface must be changed accordingly.
Here's one of the most popular examples of the Web: doors and Alarms: doors have open () and close () two actions, at which point we can define this abstract concept through abstract classes and interfaces:
Abstract class Door {public
abstract void Open ();
public abstract void Close ();
}
Or
Interface Door {public
abstract void Open ();
public abstract void Close ();
}
But now if we need the function of the gate with the alarm alarm (), then how to implement it. Here are two ways to do this:
1 The three functions are placed in the abstract class, but so all the subclasses inherited from this abstract class have the alarm function, but some doors do not necessarily have the alarm function;
2 The three functions are placed in the interface, the need to use the alarm function of the class need to implement the interface Open () and close (), perhaps this class does not have the open () and close () these two functions, such as fire alarm.
It can be seen from here that the door open (), close () and alarm () are essentially two different categories of behavior, open () and close () are intrinsic to the door itself, and alarm () is an extension of the additional behavior. The best solution, therefore, is to design the alarm individually as an interface, containing the alarm () behavior, door designed as a separate abstract class that contains open and close two behaviors. Then design an alarm door to inherit the door class and implement the alarm interface.
Interface Alram {
void alarm ();
}
Abstract class Door {
void open ();
void Close ();
}
Class Alarmdoor extends Door implements Alarm {
void Oepn () {
//...
}
void Close () {
//...
}
void Alarm () {
//...
}
}
4. Summary
1. Abstract classes and interfaces cannot be instantiated directly, and if instantiated, the abstract class variable must point to a subclass object that implements all of the abstract methods, and the interface variable must point to the class object that implements all the interface methods.
2, the abstract class to inherit the quilt class, the interface to be implemented by the class.
3, the interface can only do the method declaration, the abstract class can make the method affirmation, also can do the method realization
4. The variables defined in the interface can only be public static constants, and the variables in the abstract class are ordinary variables.
5. Abstract methods in an abstract class must all be implemented by the quilt class, which can only be an abstract class if the subclass cannot implement the parent-class abstract method in its entirety. Similarly, when an interface is implemented, it can only be an abstract class if the interface method cannot be implemented in its entirety.
6, the abstract method can only affirm, cannot realize. abstract void ABC (); Cannot write abstract void abc () {}.
7, abstract class can be 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, and can not be private.
10, interface can inherit interface, and can inherit interface more, but the class can inherit only single root. Abstract classes have its advantages, especially for common implementation code. Abstract classes can guarantee the hierarchical relationship of implementation and avoid code duplication. However, even in situations where abstract classes are used, the principle of defining behavior models through interfaces is not overlooked. From a practical point of view, if you rely on abstract classes to define behavior, often lead to overly complex inheritance relationships, and through interface definition behavior can be more effective separation of behavior and implementation, for the maintenance and modification of code to bring convenience.
Reference:
Http://www.cnblogs.com/vamei/archive/2013/03/31/2982240.html
Http://www.cnblogs.com/dolphin0520/p/3811437.html
Http://www.cnblogs.com/azai/archive/2009/11/10/1599584.html
Http://www.cnblogs.com/xdp-gacl/p/3651121.html
Http://www.cnblogs.com/vamei/archive/2013/03/27/2982230.html