java extensions question

Asked 2 years ago, Updated 2 years ago, 25 views

Hi, everyone. I'm learning the extensions part, but I don't understand the XYZ class part. Does Xxyz in XYZ class mean calling Z()? I tried to make it work X Y Z I saw the printout, but the execution process is a little confusing.

java

2022-09-22 18:20

1 Answers

Automatically insert super() in the first line of the child class constructor. This means that each constructor calls the constructor of the parent class, so it runs from the constructor of the top parent.

The easiest way to check is to disassemble the class to verify the byte code. It's not that difficult to read JVM's byte code, so taking the time to learn can help you judge performance issues or ambiguous situations.

class Parent {
    public Parent() {
        System.out.println("Parent");
    }
}

class Son extends Parent {
    public Son() {
        System.out.println("Son");
    }
}

public class Main2 {
    public static void main(String args[]) {
        Son son = new Son();
    }
}

// When you see the byte code directly, the constructor calls the parent constructor. This part is automatically added.

[allinux@lghnh ~]$ javap -c Son.class
Compiled from "Main2.java"
class Son extends Parent {
  public Son();
    Code:
       0: aload_0
       1: invokespecial #1 // MethodParent."<init>":()V <- Called parent class constructor
       4: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
       7: ldc           #3                  // String Son
       9: invokevirtual #4                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
      12: return
}


2022-09-22 18:20

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.