Abstract classes are a happy medium between ordinary classes and interfaces. Although our first thought when building a class with some implementation method might be to create an interface, abstract classes are still an important and necessary tool for this purpose. Because you can’t always use pure interfaces.

—- Java programming ideas

Abstract Class Overview

An abstract class is a class that is decorated with the abstract keyword

Abstract Class characteristics

/ * * *@description: Abstract book *@author: candou
 * @create: the 2021-06-28 foreign * /
public abstract class BookContent {

    /** * Abstract method to connect different three parties to save books */
    public abstract void saveBook(a);

    /** * List of common method books */
    public void bookList(a) {
        System.out.println("Show book list"); }}Copy the code
1. Abstract classes and methods must be decorated with the abstract keyword

Public abstract class name {} public abstract void method name ();

2. An abstract class does not necessarily have an abstract method, but a class with an abstract method must be an abstract class
public abstract class ThirdPartyTwo extends BookContent {
}
Copy the code

If there are only abstract methods in an abstract class, it is recommended to use an interface instead (the interface is an extremely abstract class).

3. Abstract classes cannot be instantiated

How can an abstract class be instantiated? Instantiation by subclass objects in the same way as polymorphism, also called abstract class polymorphism

4. Subclasses of abstract classes

Either override all the abstract methods in the abstract class, or the abstract class

/ * * *@description: Book source docking implementation *@author: candou
 * @create: the 2021-06-29 00:35 * /
public class ThirdPartyOne extends BookContent {
    @Override
    public void saveBook(a) {
        System.out.println("The first book source."); }}Copy the code
/ * * *@description: Book source docking *@author: candou
 * @create: the 2021-06-29 00:36 * /
public abstract class ThirdPartyTwo extends BookContent {}Copy the code
5. The differences between abstract classes and ordinary classes

1) An abstract class cannot be instantiated, that is, an object cannot be created using the new keyword. Its purpose is to act as a parent class and be inherited by subclasses. 2) An abstract class contains (but does not have to) abstract methods, which must be defined in the abstract class

/ * * *@description: Book related tests *@author: candou
 * @create: the 2021-06-29 00:46 * /
public class BookDemo {

    public static void main(String[] args) {
        BookContent bookContent = new BookContent() {
            @Override
            public void saveBook(a) {
                System.out.println("Book preservation"); }}; BookContent bookContent2 =newThirdPartyOne(); bookContent.saveBook(); bookContent2.saveBook(); }}Copy the code
Books preserve the first source of booksCopy the code

Application scenarios of abstract classes

1. Define the abstractness of classes

Abstract classes tell users and compilers how to use them. For example, in the above example, when there are multiple book sources, it is difficult to ensure that the format provided by these book sources is the same, so it is necessary to write different processing logic for different sources, so as to convert them into the format required in our database for storage. Whenever a new source needs to be docked, the BookContent inherits after the new class is created and you know that you need to rewrite the saving method of the book.

Refactor your code

Abstract classes make it easy to extract public methods along inheritance relationships.

3. Template design mode

In the example above, the BookContent abstract class is a template that defines two methods for books and deferred the implementation of the save methods to subclasses. This allows subclasses to redefine the implementation logic of the save method without changing the overall structure.