Interface, abstract class and implementation class of java foundation

interface

Abstraction of behavior

Abstract class

 

Implementation class

Method of implementing interface definition

The difference between interface and abstract class

1. The difference on the grammatical level

  1) Abstract classes can provide the implementation details of member methods, but only public abstract methods can exist in interfaces;

  2) The member variables in the abstract class can be of various types, while the member variables in the interface can only be of public static final type;

  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 only inherit one abstract class, but a class can implement multiple interfaces.

2. The difference in design

  1) An abstract class is an abstraction of a kind of thing, that is, an abstraction of a class, and an interface is an abstraction of behavior. An abstract class abstracts the entire class as a whole, including attributes and behaviors, but an interface abstracts part of the class (behavior). To give a simple example, airplanes and birds are different kinds of things, but they all have one thing in common, that is, they can both fly. Then, when designing, you can design the airplane as a class of Airplane and the bird as a class of Bird, but you cannot design the characteristic of flying as a class, so it is only a behavioral characteristic, not an abstract description of a class of things. . At this point, the flight can be designed as an interface Fly, including the method fly( ), and then Airplane and Bird implement the Fly interface according to their own needs. Then, as for different types of aircraft, such as fighter jets and civil aircraft, you can directly inherit the Airplane. The same is true for birds. Different types of birds can directly inherit the Bird class. It can be seen from this that inheritance is a relationship of "is it right", while interface implementation is a relationship of "is there or not". If a class inherits an abstract class, the subclass must be the type of abstract class, and the interface implementation is related to whether or not it has the relationship, such as whether a bird can fly (or whether it has the characteristic of flying) and can fly You can implement this interface, but you won’t implement this interface if you can’t fly.

  2) The design level is different. As the parent class of many subclasses, the abstract class is a template design. The interface is a code of conduct, it is a radial design. What is template design? The simplest example, everyone has used the templates in ppt. If you use template A to design ppt B and ppt C, the common part of ppt B and ppt C is template A. If their public parts need to be changed, you only need to change Template A is fine, and there is no need to change ppt B and ppt C again. Radial design, for example, a certain elevator is equipped with a certain alarm, once the alarm is to be updated, it must be updated all. That is to say, for abstract classes, if you need to add new methods, you can directly add specific implementations in the abstract class, and the subclass can be left unchanged; but for the interface, it doesn’t work. If the interface is changed, all implementations of this interface The classes must be changed accordingly.

Reference link

In-depth understanding of Java's interfaces and abstract classes

Guess you like

Origin blog.csdn.net/Candyys/article/details/109535170