Abstract is one of the most important features of object-oriented programming. In Java, there are two ways to embody the abstraction of OOP: interfaces and abstract classes. There are too many similarities, and there are too many different places. Many people think they can be used interchangeably when they are beginners, but not in practice. Today we will learn about interfaces and abstract classes in Java. The following is the directory outline for this article:
I. Abstract class
Two. Interface
Three. The difference between abstract classes and interfaces
If there are any shortcomings, please understand and welcome the criticism, not very grateful.
Please respect the author's labor results, reproduced please indicate the original link:
Http://www.cnblogs.com/dolphin0520/p/3811437.html
I. Abstract class
Before you learn about abstract classes, take a look at the abstract methods. An abstract method is a special method: it is only declared, and there is no specific implementation. The declarative format for an abstract method is:
Abstract methods must be decorated with the abstract keyword. If a class contains abstract methods, it is called an abstract class, and the abstract class must be decorated with the abstract keyword before the class. Because abstract classes contain methods that are not specifically implemented, you cannot create objects with abstract classes.
Here's a question: In the Java programming idea, the abstract class is defined as "the class containing the abstract method", but later it is discovered that if a class does not contain an abstract method, it is also an abstract class if it is simply decorated with an abstraction. That is, abstract classes do not necessarily have to contain abstract methods. Personally think this belongs to the problem of the dead, because if an abstract class does not contain any abstract method, why design as abstract class? So remember this concept for a while, without having to delve into why.
|
[public] abstractclass ClassName { abstractvoidfun();} |
As can be seen here, abstract classes exist for inheritance, if you define an abstract class, but do not inherit it, it is tantamount to creating this abstract class in vain, because you can not use it to do anything. For a parent class, if one of its methods is implemented in the parent class without any meaning, it must be implemented differently according to the actual needs of the subclass, then the method can be declared as an abstract method, and this class becomes the abstract class.
A class that contains an abstract method is called an abstract class, but it does not mean that there can be only abstract methods in an abstract class, which, like normal classes, can also have member variables and ordinary member methods. Note that there are three main differences between abstract and ordinary classes:
1) The abstract method must be public or protected (because if you are private, you cannot inherit from the quilt class, the subclass cannot implement the method), and by default it is public.
2) Abstract classes cannot be used to create objects;
3) If a class inherits from an abstract class, the child class must implement the abstract method of the parent class. If the subclass does not implement an abstract method of the parent class, the subclass must also be defined as an abstract class.
In other respects, there is no difference between an abstract class and an ordinary class.
Two. Interface
Interface, English is called interface, in software engineering, the interface refers to the method or function for others to call. From here, we can realize the original intention of the Java language Designer, which is the abstraction of the behavior. In Java, the form of an interface is as follows:
|
[public] interfaceInterfaceName {} |
The interface can contain variables and methods. Note, however, that the variables in the interface are implicitly specified as public static final variables (and can only be public static final variables, which are reported as compilation errors with private adornments), and methods are implicitly specified as public Abstract method and can only be public abstract method (with other keywords, such as private, protected, static, final and so on will be reported compile errors), and the interface of all methods can not have a concrete implementation, that is to say, The methods in the interface must all be abstract methods. From here we can see the difference between interface and abstract class, interface is an extremely abstract type, it is more "abstract" than abstract class, and generally does not define variables in the interface.
To have a class follow a specific set of interfaces requires the use of the Implements keyword, in the following format:
|
classClassName implementsInterface1,Interface2,[....]{} |
As you can see, a class is allowed to follow multiple specific interfaces. If a non-abstract class follows an interface, you must implement all the methods in that interface. Abstract methods in the interface may not be implemented for abstract classes that follow an interface.
Three. The difference between abstract classes and interfaces
1. Differences in the grammatical level
1) Abstract classes can provide implementation details of member methods, and only public abstract methods exist in interfaces;
2) member variables in an abstract class can be of various types, whereas member variables in an interface are only 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.
2. Differences at the design level
1) Abstract class is an abstraction of a thing, that is, an abstraction of a class, and an interface is an abstraction of a behavior. An abstract class is an abstraction of the whole class as a whole, including properties, behaviors, but an interface that abstracts the local (behavior) of a class. For a simple example, airplanes and birds are different kinds of things, but they all have a common denominator, that is, they can fly. Then in the design, the aircraft can be designed as a class airplane, the bird is designed as a class bird, but not the characteristics of the flight is also designed as a class, so it is only a behavioral characteristics, not a kind of abstract description of things. At this point the flight can be designed as an interface fly, including the method fly (), and then airplane and bird respectively according to their own needs to implement the fly this interface. Then as for the different types of aircraft, such as fighter jets, civil aircraft, such as direct inheritance airplane can, for birds is similar, different species of birds directly inherit the bird class can be. As can be seen from here, inheritance is a "is not" relationship, and the 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 implementation of the interface is there is no, with no relationship, such as whether the bird can fly (or whether it has the characteristics of flight), can fly can realize this interface, not flying will not implement this interface.
2) The design level is different, abstract class as the parent class of many subclasses, it is a kind of template design. And the interface is a kind of behavior specification, it is a kind of radiant design. What is a template design? The simplest example, we have used the template in ppt, if the template a designed ppt B and ppt c,ppt B and ppt C public part is the template A, if their public parts need to change, then only need to change the template A can be, do not need to re-ppt B and ppt C changes. and radiation design, such as an elevator are installed some kind of alarm, once to update the alarm, you must update all. That is, for an abstract class, if you need to add a new method, you can directly add a concrete implementation in the abstract class, the subclass can not be changed, but for the interface is not, if the interface has been changed, all implementations of this interface must be modified by the corresponding class.
Here's an example of the most widely circulated online: doors and Alarms: doors have open () and close () two actions, at which point we can define this abstract concept through abstract classes and interfaces:
|
abstractclass Door { public abstract void open(); publicabstract voidclose();} |
Or:
|
interfaceDoor { public abstract void open(); publicabstract voidclose();} |
But now if we need the door to have the function of alarm alarm (), then how to implement? Here are two ideas:
1) These three functions are placed in the abstract class, but so that all the subclass inherited from the abstract class has an alarm function, but some doors do not necessarily have the alarm function;
2) The three functions are placed in the interface, need to use the alarm function of the class need to implement the interface of open () and close (), perhaps this class does not have open () and close () these two functions, such as fire alarm.
As can be seen here, Door's open (), close () and alarm () are essentially two different categories of behavior, and open () and close () are intrinsic behavior characteristics of the gate itself, and alarm () is an extension of the additional behavior. So the best solution is to individually design the alarm as an interface that contains the alarm () behavior, door is designed as a separate abstract class that contains open and close two behaviors. Then design an alarm gate to inherit the door class and implement the alarm interface.
|
interfaceAlram { voidalarm();}abstractclassDoor { voidopen(); voidclose();}classAlarmDoor extendsDoor implementsAlarm { voidoepn() { //.... } voidclose() { //.... } voidalarm() { //.... }} |
Comparison of abstract classes and interfaces
Parameters |
Abstract class |
Interface |
The default method implementation |
It can have the default method implementation |
The interface is completely abstract. There's no way to implement it at all. |
Realize |
Subclasses use the extends keyword to inherit abstract classes. If the subclass is not an abstract class, it needs to provide an implementation of all the declared methods in the abstract class. |
Subclasses use the keyword implements to implement an interface. It needs to provide an implementation of all the declared methods in the interface |
Constructors |
Abstract classes can have constructors |
Interfaces cannot have constructors |
Differences from normal Java classes |
Except that you can't instantiate an abstract class, it's no different than a normal Java class. |
Interfaces are completely different types |
Access modifiers |
Abstract methods can have public,protected , and default modifiers |
The interface method default modifier is public. You may not use other modifiers. |
Main method |
The abstract method can have the main method and we can run it |
The interface does not have a main method, so we cannot run it. |
Multiple inheritance |
An abstract method can inherit a class and implement multiple interfaces |
Interfaces can inherit only one or more other interfaces |
Speed |
It's faster than the interface. |
The interface is a little bit slower because it takes time to look for methods implemented in the class. |
Add a new method |
If you add a new method to an abstract class, you can give it a default implementation. So you don't need to change your current code. |
If you add a method to an interface, you must change the class that implements the interface. |
When to use abstract classes and interfaces
- If you have some methods and want some of them to have default implementations, then use abstract classes.
- If you want to implement multiple inheritance, then you must use the interface. Because Java does not support multiple inheritance , subclasses cannot inherit multiple classes, but can implement multiple interfaces. So you can use the interface to solve it.
- If the basic ability is constantly changing, then you need to use abstract classes. If you constantly change the basic functionality and use the interface, you need to change all classes that implement the interface.
Deep understanding of Java interfaces and abstract classes