A person's excellence stems from its continuous learning, as long as the goal to find their own, and to continue to work towards this goal, perseverance and not slack, success will be a step closer to you.
Abstract classes and interfaces are two different abstract concepts in the Java language, and their presence provides very good support for polymorphism, although there is a great similarity between them. But for their choices, it often reflects your understanding of the problem domain. Only a good understanding of the nature of the problem domain can make the correct and reasonable design.
First, abstract class
In the field of object-oriented, everything is an object. All objects are described by class, but not all classes are used to describe objects. If a class does not have enough information to describe a specific object and needs other concrete classes to support it, then such a class is called an abstract class. As New Animal (), we know that this code is intended to produce a Animal object, but Animal exactly what it looks like, we are not sure, because there is no specific Animal concept, so Animal is an abstract class. It takes a specific animal, such as a cat, a dog, to describe it in a specific way, so that we can specifically implement its example.
Abstract class embodies the idea of data abstraction and is a mechanism to realize polymorphism. It defines a set of abstract methods that are implemented by derived classes as a concrete representation of an abstract method. At the same time, abstract class provides the concept of inheritance, its starting point is to inherit, otherwise it will not have the meaning of existence. Therefore, the definition of abstract classes must be used to inherit.
1. Abstract classes cannot be instantiated.
2. An abstract class can contain 0 or more abstract methods, 0 or more common methods, and 0 or more member variables.
3. If a non-abstract subclass inherits the abstract parent class, all the abstract methods in the parent class must be overridden and implemented, and the normal method can not be overridden.
4. Abstract subclasses inherit the abstract parent class, and the abstract method of the parent class can be partially rewritten.
5. A class that contains one or more abstract methods must be defined as an abstract class.
6.abstract and final cannot modify the same class at the same time.
7.abstract cannot modify the same method at the same time as private, static, final, native.
Second, the interface
An interface is an encapsulation of a set of methods, and an interface defines a set of methods, but none of these methods are specifically implemented. Multiple inheritance is not supported in Java (because if you inherit more than one parent class, there is confusion if the same variable name and method name exist in different parent classes.) ), but a class can implement multiple interfaces. All methods in 1.interface are automatically declared public and must be public. The member variables in 2.interface must be public static final. 3. The implementation method is not allowed in the interface. 4. A non-abstract class that implements an interface must implement all the methods in the interface, but an abstract class can implement some of the methods. 5. The methods in different interfaces may have duplicate names.when a class implements multiple interfaces, the workaround is to use a private inner class (an external class implements one of the interfaces, and an inner class implementation makes an interface).
Three, the difference between abstract class and interface
1. From the syntax: 1) Abstract classes can contain ordinary member variables, with an implementation of the ordinary method, and the interface can only have public static final member variables, not a concrete implementation of the method. 2) A class can inherit only one abstract class (single inheritance), but it is possible to implement multiple interfaces. 2. From the design point of view: 1) different levels of abstraction. An abstract class is an abstraction of a class, and an interface is an abstraction of the behavior. 2) different across domains. An abstract class is a public part that is discovered from a subclass and then abstracted as an abstract class. The implementation subclass of an interface can have no relationship, just implement a common method in the interface. such as cats and dogs are animals, all have the method called, then the animal can be defined as abstract class, with the abstract method called. Birds and airplanes all have flying behavior, and they can all be implemented from an interface that defines the method of flying.(abstract class embodies an inheritance relationship, the inheritance relationship must exist "is a" relationship.) )
四、一个 example of a door with alarm function
We have an abstract concept of door, which has 2 behaviors open (), close (), at which point we can define this abstract concept through abstract classes and interfaces: Abstract classes:
1 Abstract class door{2 Abstract void open (); 3 Abstract void close (); 4 }
Interface:
1 Interface door{2 void open (); 3 void close (); 4 }
Now, there's no difference between the two. If you need to add an alarm function, how to implement it? Scenario One: Add a alarm () method for the above abstract classes and interfaces, but this method violates the interface isolation principle and the open and closed principle in object-oriented design. and may lead to some other implementation class and module changes, it is not advisable. Scenario two: 1. Two are defined using an abstract class. 2. Two are defined using an interface. 3. One is defined using an abstract class, and one is defined using an interface. Because Java does not support multiple inheritance, the first method is not feasible. In common sense, our understanding of the problem is this: Alarmdoor is essentially door, and it has the function of alerting. Therefore, the 3rd method is more able to articulate our design intent.
1 Abstract classdoor{2 Abstract voidopen ();3 Abstract voidclose ();4 }5 6 Interfacealarm{7 voidalarm ();8 }9 Ten classAlarmdoorextendsDoorImplementsalarm{ One voidopen () {} A voidclose () {} - voidalarm () {} -}
The difference between an interface and an abstract class in Java