Simple understanding of Java's abstract classes

  • 2020-04-01 04:13:34
  • OfStack

In a top-down inheritance hierarchy, classes at the top are more generic and possibly even more abstract. In some ways, the ancestor class is more generic in that it contains only the most basic members and is only used as a base class to derive other classes, not to create objects. You can even define methods without implementing them, and let subclasses implement them according to their specific requirements.

This method that only gives the method definition without concrete implementation is called abstract method. Abstract method has no method body, and there is no "{}" in the expression of code. Classes containing one or more abstract methods must also be declared as abstract classes.

The abstract modifier is used to represent abstract methods and abstract classes.

Abstract classes can contain concrete variables and concrete methods in addition to abstract methods. Classes can be declared as abstract classes even if they do not contain abstract methods, preventing them from being instantiated.

Abstract classes cannot be instantiated, and abstract methods must be implemented in subclasses. Look at 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
abstract class People{
  private String name; //The instance variables
  
  //Common setter and getter methods
  public void setName(String name){
    this.name = name;
  }
  public String getName(){
    return this.name;
  }
  
  //Abstract methods
  public abstract void work();
}
class Teacher extends People{
  //This method must be implemented
  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'm driving and I can't answer the phone ...");
  }
}

Operation results:


 My name is wang Ming. I'm giving a lecture. Please don't look around ...
 My name is Chen. I'm driving and I can't answer the phone ...

A few notes on abstract classes:
Abstract classes cannot be used directly. You must implement an abstract class with a subclass and then use an instance of that subclass. However, you can create a variable whose type is an abstract class and point it to an instance of a concrete subclass, which means you can use the abstract class as a parameter and actually implement the class as an argument, which is the application of polymorphism.
You cannot have an abstract constructor or an abstract static method.


A class becomes an abstract class in the following cases:
When one or more methods of a class are abstract methods;
When the 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 bodies for any abstract method; Note:
What we're saying here is that in these cases a class is going to be an abstract class, we're not saying that an abstract class is necessarily going to have these cases.
A typical error: an abstract class must contain abstract methods. But the reverse is true to say that a class that contains an abstract method must be an abstract class.
In fact, an abstract class can be a perfectly normal implementation of a class


Related articles: