An abstract class
If a class has abstract methods (only the method signature, but no method implementation), then the class must be modified by abstract. This class is called abstract class, and abstract class can have no abstract methods. Abstract class features:
- Abstract classes must be modified with the Abstract modifier, and abstract methods must also be modified with the Abstract modifier. Abstract methods cannot have a method body.
- Abstract classes cannot be instantiated. You cannot create an instance of an abstract class using the new keyword by calling the constructor of the abstract class. Even if an abstract class does not contain abstract methods, the abstract class cannot create instances.
- Abstract classes can contain member variables, methods (ordinary methods and abstract methods can be), constructors, initialization blocks, inner classes (interface, enumeration). The constructor of an abstract class cannot be used to create an instance, but is primarily used to be called by its subclasses.
- Classes that contain abstract methods (including those that directly define an abstract method; Or inherits an abstract superclass, but does not fully implement the abstract methods that the superclass contains; Or implements an interface, but does not fully implement the abstract methods that the interface contains.
Abstract class vs. ordinary class:
- Abstract classes can contain abstract methods,
- Abstract classes cannot be used to create instances.
Abstract class reflects the design idea of a template pattern. Abstract class serves as a general template for multiple subclasses. Subclasses extend and transform on the basis of abstract class, but generally retain the behavior of abstract class. (A subclass can implement an abstract method of its abstract superclass. It does not need to implement all of the abstract methods. In this case, the subclass must be modified by Abstract.)
interface
An interface defines a common behavior common to multiple classes that act as a channel to communicate with the outside world, usually by defining a common set of methods that must be implemented. Because an interface is defined as a specification, it cannot contain constructor and initializer block definitions.
- Interfaces can contain member variables (static constants only), methods (abstract instance methods only), and class methods (static modified by the class name). Method name call) or default method default), inner class (including internal interface, enumeration) definition.
- All members of the interface have public access rights.
- All member variables defined in the interface are decorated with public static final by default.
- The ordinary methods in the interface must be abstract methods (implicitly declared public abstract), that is, there is no method implementation; But class methods (static) and default methods (default) must be implemented.
The difference between interfaces and abstract classes
Interface, as a window for the system to interact with the outside world, embodies a specification. When used in a program, interfaces are the standard for coupling between modules. When used between multiple applications, an interface is the standard for communication between multiple programs.
Abstract class, as the common parent of several subclasses in the system, embodies a template design. As the abstract parent of multiple subclasses, an abstract class can be considered as an intermediate product in the system implementation process. This intermediate product already implements some functions of the system (those that already provide implementation methods), but this product is not the final product and needs further refinement.
• Interfaces can contain only abstract and default methods and class methods, and cannot provide implementations for ordinary methods; Abstract classes can contain ordinary methods. • Interfaces cannot define static methods (after Java8 you can (class methods); Static methods can be defined in abstract classes. • The interface can only define static constants, not ordinary member variables; Abstract classes can define ordinary member variables as well as static constants. • The interface contains no constructors; An abstract class can contain constructors that, instead of creating objects, have subclasses call these constructors to perform initialization operations that belong to the abstract class. • The interface cannot contain initialization blocks; But an abstract class can contain an initialization block. • A class can have at most one direct parent, including an abstract class; But a class can directly implement multiple interfaces, by implementing multiple interfaces can make up for Java single inheritance.Copy the code
When to use abstract classes and interfaces
• If you want to implement multiple inheritance, you must use interfaces. Because Java does not support multiple inheritance, subclasses cannot inherit multiple classes, but can implement multiple interfaces. • Use abstract classes if the basic functionality is changing. If an interface is used, all classes that implement the interface are overridden when functionality changes. • The abstract class is a and the interface is aCopy the code