First, abstract class
- Classes decorated with the abstract keyword are called abstract classes, and abstract methods are called abstractions.
- The abstract method must be public or protected, and the default is public.
- Classes containing abstract methods must be declared as abstract classes, abstract classes must be inherited, abstract methods must be overridden
- Abstract classes cannot be instantiated
- Abstract methods need to be declared without implementing
Second, the interface
Defined:
- An interface (interface) is a collection of abstract methods and constant value definitions.
- In essence, an interface is a special kind of abstract class that contains only the definitions of constants and methods, without the implementation of variables and methods.
- Interface definition: the member variables in the interface are all public static final ; The methods in the interface are all public abstract
Characteristics:
- Multiple unrelated classes can implement the same interface
- A class can implement multiple unrelated interfaces
- Like inheritance, there is polymorphism between the interface and the implementation class.
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:
| 1 2 3 4 |
abstract class Door { public abstract void open(); public abstract void close();} |
Or:
| 2 " Span style= "FONT-SIZE:16PX;" >3 4 |
interface door {      public abstract void open ();      public abstract void close (); } |
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.
| 1 2 3 4 5 6 7 8 9 Ten One A - - the - - - + - |
interface Alram { void alarm();} abstract class Door { void open(); void close();} class AlarmDoor extends Door implements Alarm { void oepn() { //.... } void close() { //.... } void alarm() { //.... }} |
Java abstract classes and interfaces