Consider why these two technologies are needed first from the design requirements.
Abstract class, first class. A class is a definition of an object that indicates that an object has specific properties and behaviors, such as a dog barking and a dog barking as a specific behavior. Abstract classes, like their names, represent abstractions of objects, and ordinary classes are used to represent specific objects, such as dogs and cats, but the way they are called is different. Because it is not possible to represent a specific object, it makes no sense to instantiate it. Abstract class Usage principles:
- Abstract classes must not be instantiated (design requirements and syntax)
- Abstract classes are bound to be inherited (design requirements)
- Abstract classes generally contain abstract methods (design requirements)
- Subclasses are still abstract classes (design requirements and syntax) if they do not have all the abstract methods of the Override abstract class
- Abstract classes are modified with an abstract and cannot be present at the same time as final (design requirement syntax)
- Abstract methods are not available with the private static final native (design requirement syntax) with abstract modification
It can be seen that the limitations of grammar are also designed to ensure the implementation of design requirements. Examples of use of abstract classes:
Abstract class Animal () { abstractvoid Cry ();} class extends Animal () { @Override void Cry () { }}classextends Animal () { @Override void Cry () { }}
interface, which is used to represent the collection of behaviors. In a specific application, if a class inherits an abstract class, it means that the class is a special case of the abstract class, whereas a class implements an interface, which means that the class has the behavior defined in the interface.one is "yes" and one is "yes", which is why inheritance in Java is single-inheritance, and implementation can be multi-implementation. For example, the door, some have a doorbell, some do not have a doorbell, this is some relationship, according to the doorbell this operation, the use of interface is more appropriate. Interface usage Rules:
- The properties in the interface are all modified by public static final, so there is no need to display the write
- The methods in the interface are all modified by public, so there is no need to display write
- For a class to implement an interface, it is necessary to implement all the methods in this interface
- A class can implement multiple interfaces at the same time
Examples of simultaneous use of interfaces and abstract classes:
Abstract classDoor () {Abstract voidopen ();}InterfaceAlarm () {voidalarm ();}classNormaldoorextendsDoor () {@Overridevoidopen () {}}classAlarmdoorextendsDoorImplementsAlarm () {@Overridevoidopen () {} @Overridevoidalarm () {}}
From for notes (Wiz)
Abstract classes and Interfaces