In depth understanding of the Java parent and subclass initialization order

  • 2020-04-01 02:00:37
  • OfStack

The process of building an inherited class is diffused, the subclass is initialized, and the parent must be initialized as well

class Art{
 Art(){
  System.out.println("Art");
 }
}
class Drawing extends Art{
 Drawing(){
  System.out.println("Drawing");
 }
}
public class Cartoon extends Drawing {
 Cartoon(){
  System.out.println("Cartoon");
 }
 public static void main(String[] args) {
  Art cartoon = new Cartoon();
 }
}

The output is:

Art
Drawing
Cartoon


Related articles: