Java basic abstract class study notes

1. What is an abstract class

A class does not have enough information to describe a specific object and needs to rely on other specific classes to support it. This class is called an abstract class.
For example: animal class, but we don’t know what this Animal looks like. It doesn’t have the concept of a specific animal, so it is an abstract class that needs a specific animal, such as a dog or a cat, to describe it specifically. .

2. Introduction to abstract classes

  1. When a class is modified with the abstract keyword, the class is called an abstract class—>public abstract Animal { }
  2. When using the abstract keyword to modify a method, this method is an abstract method—> public abstract void eat(String str); //There is no method body (a class with an abstract method must be an abstract class)
  3. The value of the abstract class lies more in the design. After the designer designs it, let the subclass inherit and implement the abstract class

3. Details of the use of abstract classes

1. Abstract classes cannot be instantiated (cannot be new)
2. Abstract classes do not necessarily contain abstract methods. In other words, an abstract class can have no abstract method
3. Once a class contains an abstract method, the class must be declared as abstract
4. abstract can only modify classes and methods, not attributes and others
5. Abstract classes can have any members [The essence of an abstract class is still a class], such as: non-abstract methods, constructors, static properties, etc.
6. Abstract methods cannot have a main method body
7. Abstract methods cannot be decorated with private, final, and static, because these keywords are Contrary to rewriting (abstract methods are meant to be rewritten)
8. If a class inherits an abstract class, it must implement all abstract methods of the abstract class, unless it declares itself as an abstract class

Guess you like

Origin blog.csdn.net/cc1373709409/article/details/123096436