In the top-down inheritance hierarchy, classes located at the upper level are more general and may even be more abstract. From a certain perspective, ancestor classes are more general, they only contain some of the most basic members, and people only use it as the base class for deriveing other classes, and will not be used to create objects. Even, you can only give the definition of the method without implementing it, and the subclasses implement it according to specific needs.
This method that only gives method definitions but does not implement concretely is called an abstract method. Abstract methods have no method body, and there is no "{}" in the expression of the code. Classes containing one or more abstract methods must also be declared as abstract classes.
Use the abstract modifier to represent abstract methods and abstract classes.
In addition to containing abstract methods, abstract classes can also contain specific variables and specific methods. Even if a class does not contain abstract methods, it can be declared as an abstract class to prevent it from being instantiated.
Abstract classes cannot be instantiated, abstract methods must be implemented in subclasses. Please see the following code:
import static java.lang.System.*;public final class Demo{ public static void main(String[] args) { Teacher t = new Teacher(); t.setName("Wang Ming"); t. work(); Driver d = new Driver(); d.setName("Xiao Chen"); d.work(); }}// Define an abstract class People{ private String name; // Instance variable// Common setters and getter method public void setName(String name){ this.name = name; } public String getName(){ return this.name; } // Abstract method public abstract void work() ;}class Teacher extends People{ // Must be implemented This method public void work(){ out.println("My name is" + this.getName() + ", I'm giving a lecture, please don't look around..."); }}class Driver extends People{ // This method must be implemented public void work(){ out.println("My name is" + this.getName() + ", I am driving and can't answer the call..."); }}
Running results:
My name is Wang Ming, I am giving a lecture, please don’t look around... My name is Xiao Chen, I am driving and can’t answer the phone...
A few explanations about abstract classes:
Abstract classes cannot be used directly. You must use subclasses to implement abstract classes and then use instances of their subclasses. However, a variable can be created whose type is an abstract class and let it point to an instance of a concrete subclass. That is, an abstract class can be used as a formal parameter, and the actual implementation of the class as a real parameter, that is, the application of polymorphism.
There cannot be abstract construction methods or abstract static methods.
A class will become an abstract class in the following cases:
When one or more methods of a class are abstract methods;
When a class is a subclass of an abstract class and cannot provide any implementation details or method body for any abstract method;
When a class implements an interface and cannot provide implementation details or method body for any abstract method; note:
What we are talking about here is that in these cases a class will become an abstract class, and it is not said that abstract class will definitely have these situations.
A typical error: an abstract class must contain abstract methods. But on the other hand, "a class containing abstract methods must be an abstract class" is correct.
In fact, an abstract class can be a completely normal class