Abstract class(Recommended learning:java course)
Abstract classes must be used Modification, the subclass must implement the abstract method in the abstract class. If there are unimplemented methods, the subclass must also be decorated with abstract. The default permission modifier of an abstract class is public, which can be defined as public or procted. If it is defined as private, subclasses cannot inherit it. Abstract classes cannot create objects
The difference between abstract classes and ordinary classes
Abstract classes must be modified with public and procted (if modified with private, subclasses cannot inherit , and its abstract method cannot be implemented). The default is public
Abstract classes cannot create objects
If a subclass inherits an abstract class, it must implement all its abstract methods. If there are unimplemented abstract methods, they must be defined as abstract
Interface
The variables in the interface are implicitly modified with public static final, and an initialization needs to be given value.
The method is implicitly modified with public abstract (and it can only be public. If it is private or procted, an error will be reported during compilation).
Methods in interfaces cannot have specific implementations by default (default implementations are available starting from JDK1.8)
The difference between interfaces and abstract classes
Abstract classes can only be inherited once, but can implement multiple interfaces
Interfaces and abstract classes must implement all methods in them. If there are unimplemented abstract methods in the abstract class , then the subclass also needs to be defined as an abstract class. Abstract classes can have non-abstract methods
Variables in the interface must be modified with public static final, and an initial value needs to be given.So the implementation class cannot be redefined, nor can its value be changed.
The methods in the interface default to public abstract and can only be of this type.It cannot be static. Methods in interfaces are not allowed to be overridden by subclasses. Static methods are allowed in abstract classes.
The above is the detailed content of The difference between interface and abstract class in java. For more information, please follow other related articles on the PHP Chinese website!