Abstract methods and abstract classes must be defined using the abstract modifier, and classes with abstract methods can only be defined as abstract classes, and abstract classes may not have abstract methods. Abstract classes cannot be instantiated.
An interface is just a specification that defines the specifications that a batch of classes need to follow, the interface does not care about the internal state data of these classes, and does not care about the implementation details in these classes, it only stipulates that these classes must provide certain methods. The interface embodies the design philosophy of specification and realization separation.
Interface Syntax:
- Modifiers can be public or omitted, and if the modifier is omitted, the package permission access control is used by default. So it's generally public. Because an interface is a common behavior specification for multiple classes, all members of the interface are public access rights.
- An interface can have multiple direct parent interfaces, but interfaces only inherit interfaces and cannot inherit classes.
- The interface does not contain constructors and initialization block definitions, and can contain a constant attribute/abstract instance method/inner class (internal interface/enumeration) definition.
- The constants in the interface are related to the interface, so the system automatically adds static and final modifiers to those properties. The method in the interface automatically adds an abstract adornment. And the modifier of the method can only be public and abstract
- The methods in the interface are all abstract methods, so it is not allowed to define static methods, that is, you cannot use static to modify the methods in the interface.
After a class implements one or more interfaces, the class must fully implement all of the abstract methods defined in those interfaces, otherwise the class will retain the abstract method inherited from the parent interface, which must also be defined as an abstract class.
Crazy Java Handouts (14)----abstract classes and interfaces