The answer is yes, that is, the main() method can be inherited.

Let's take a look at a specific example, first define two classes MainParent and MainChild, as follows:

MainParent.java

 package com.magic.main;

public class MainParent {

    public static void main(String[] args) {
        System.out.println("Hello, Parent");
    }
}

MainChild.java

 package com.magic.main;

public class MainChild extends MainParent {

}

Run the MainChild.java class, you can see the following output information:

 Hello, Parent

Or another way to run the MainChild.java class, as follows:

 package com.magic.main;

public class MainExtendDemo {

    public static void main(String[] args) {
        MainChild.main(args);
    }
}

Running the MainExtendDemo class will output the same information

 Hello, Parent

Therefore, it can be concluded that the main() method can be inherited, so the question comes again: Can the main() method in Java be overloaded?

For more knowledge points related to Java interviews, you can pay attention to the [Java Interview Manual] applet, involving Java foundation, multithreading, JVM, Spring, Spring Boot, Spring Cloud, Mybatis, Redis, database, data structure and algorithm, etc.


十方
226 声望433 粉丝