Home >Java >javaTutorial >How to define and use abstract classes and interfaces in java
For example, in life we all classify dogs and cats as animals, but when we only talk about animals , we don’t know if it’s a cat or a dog or something else. So animals are so-called abstract classes, and cats and dogs are concrete classes. Therefore, in Java, a method without a method body should be defined as an abstract class, and if a class has an abstract method, it must be an abstract class.
Abstract classes and abstract methods must be modified with the abstract keyword.
Abstract classes do not necessarily have abstract methods, but classes with abstract methods must be abstract classes.
Abstract classes cannot be instantiated. If they need to be instantiated, refer to the polymorphic form and instantiate through subclasses.
If a subclass inherits an abstract class, you need to rewrite all abstract methods in the abstract class, or it must be an abstract class.
Member variables: can be variables or constants.
Construction method: There is a construction method, but it cannot be instantiated, so it needs to be done through a polymorphic subclass; the purpose is to access the parent class for data initialization for the subclass.
Member method: It can be an abstract class: it limits certain behaviors that the subclass must complete; it can also be a non-abstract class, which can improve the reusability of the code.
Interface is a public specification standard, which is an additional condition required to express a class, so it can be used as long as it meets the specification standard. The interface in Java is mainly reflected in the abstraction of behavior.
needs to be modified with the keyword interface.
The implementation interface of the class needs to be represented by implements
The interface cannot be instantiated. If you want to instantiate it, refer to the polymorphic form. Instantiation is called interface polymorphism. Therefore, the main forms of polymorphism are: concrete class polymorphism, abstract polymorphism, and interface polymorphism.
Interface implementation class: Either override the abstract method in the interface, or be an abstract class.
1. Member variables: can only be constants, modified by 'public static final' by default .
2. Construction method: The interface has no construction method, because the interface mainly abstracts behavior and has no concrete existence.
Note: If a class does not have a parent class, it will inherit the Object class by default.
3. Member methods:
Abstract method: The default method of the interface is an abstract method. The default is public static abstract for modification.
Default method: The default method is the method used when some methods need to be added to the implementation class. It needs to be modified with defaul in the interface. It does not need to be rewritten in the implementation class, but When rewriting, you need to delete default.
Static method: Static methods can only be added to the interface and cannot be called by the implementation class. They can only be called by the interface name and be static modification.
Private method: Private methods are mainly used in interfaces and are modified with the private keyword. By writing the same code segment in private In the method, the reusability and simplicity of the code are improved. Static private methods can only be modified with static methods, while non-static methods can be modified with both non-static and static methods.
Abstract classes are mainly abstractions for things, interfaces are mainly abstractions for behaviors.
The above is the detailed content of How to define and use abstract classes and interfaces in java. For more information, please follow other related articles on the PHP Chinese website!