Java basics: keyword abstract

1 Overview 

  1. A method that only gives a method definition but does not implement it is called an abstract method. An abstract method has no method body. There is no "{}" in the code expression. Use abstract modifiers to represent abstract methods and abstract classes. 
  2. The abstract modifier indicates that the modified class is not fully implemented and cannot be instantiated. If the abstract modifier is used in the method declaration of the class, it indicates that the method is an abstract method and it needs to be implemented in the subclass. If a class contains abstract methods, this class is also an abstract class, the abstract modifier must be used, and it cannot be instantiated. 
  3. Note that in addition to abstract methods, abstract classes can also contain concrete variables and concrete methods. Even if a class does not contain abstract methods, it can be declared as an abstract class to prevent it from being instantiated.

2. Abstract features 

  1. The abstract class cannot be instantiated, that is, the new operator cannot be used on it; 
  2. If there are one or more abstract methods in the class, the class must be declared as abstract; 
  3. The methods in the abstract class are not necessarily all abstract methods, it can also contain one or more specific methods; 
  4. Even if a class does not contain abstract methods, it can also be declared as an abstract class; 
  5. In order to use the abstract methods in the abstract class, all the abstract methods must be copied by the subclass, and then the subclass object must be created and called. 
  6. If the subclass only covers some abstract methods, then the subclass is still an abstract class.

3. Under what circumstances, use abstract classes 

  1. The class contains a clearly declared abstract method; 
  2. Any parent class of the class contains an abstract method that is not implemented; 
  3. The direct parent interface of the class declares or inherits an abstract method, and the class does not declare or implement the abstract method.

4. A few notes about abstract classes 

  1. Abstract classes cannot be used directly. Subclasses must be used to implement abstract classes, and then instances of their subclasses must be used. However, you can create a variable whose type is an abstract class and let it point to an instance of a concrete subclass, that is, you can use the abstract class as a formal parameter and the actual implementation class as an actual parameter, which is a polymorphic application. 
  2. Constructor, static function, and final modified function cannot use abstract modifier. 
  3. If you try to create an instance of an abstract class, a compilation error will occur. 
  4. If a class is a non-abstract class but contains an abstract method, a compilation error will occur. 
  5. There are constructors in abstract classes. If the abstract class is the parent class, you need to provide instance initialization for the subclass.

5. What keywords cannot coexist with abstract keywords? 

  1. final: The class modified by final cannot have subclasses. The class modified by abstract must be a parent class. 
  2. private: Private abstract methods in abstract classes cannot be overridden without being known by subclasses. The emergence of abstract methods is that they need to be replicated. 
  3. static: If static can modify the abstract method, then even the object is saved, just call the class name directly. But the abstract method doesn't make sense to run.

The difference between abstract and interface

1. Similarities

  1. Both are abstract classes and cannot be instantiated.
  2. Both the interface implementation class and the subclasses of abstrctclass must implement the declared abstract methods.

2. Differences

  1. Interface needs to be implemented, and implements, and abstract class needs to be inherited, and extends.
  2. A class can implement multiple interfaces, but a class can only inherit one abstract class.
  3. Interface emphasizes the realization of specific functions, while abstractclass emphasizes the belonging relationship.
  4. Although both the interface implementation class and the subclasses of the abstrct class must implement the corresponding abstract methods, the implementation forms are different. Each method in the interface is an abstract method, which is only declared (declaration, no method body), and the implementation class must be implemented. The subclasses of abstractclass can be implemented selectively.

This choice has two implications: 
    First, not all methods in the Abastract class are abstract. Only those methods with abstract are abstract, and subclasses must be implemented. For those methods without abstract, the method body must be defined in the Abstrct class. 
    Second, when subclasses of abstract class inherit it, they can directly inherit or override non-abstract methods. For abstract methods, you can choose to implement them, or you can declare their methods to be abstract again, without implementing them. It is implemented by its subclasses, but this class must also be declared as an abstract class. It is an abstract class, and of course it cannot be instantiated. 

    5. Abstract class is an intermediary between interface and class. 

Interface is completely abstract, only methods can be declared, and only pulic methods can be declared, private and protected methods cannot be declared, method bodies cannot be defined, and instance variables cannot be declared. However, interface can declare constant variables, and it is not difficult to find such examples in the JDK. But putting constant variables in the interface violates the purpose of its existence as an interface, and it also confuses the different values ​​of interface and class. If you really need it, you can put it in the corresponding abstractclass or Class. 

The abstract class plays a role in connecting the previous and the next in the interface and the class. On the one hand, abstract class is abstract and can declare abstract methods to standardize the functions that subclasses must implement; on the other hand, it can define default method bodies for direct use or coverage by subclasses. In addition, it can also define its own instance variables for use by subclasses through inheritance.

Guess you like

Origin blog.csdn.net/PrisonJoker/article/details/105259463