Skip to content
Can Java's main Method Be Inherited? Alibaba Interview Deep Dive
Fundamentals 7 min read

Can Java's main Method Be Inherited? Alibaba Interview Deep Dive

This article explores three core questions about Java's main method — overloading, invocation by other methods, and inheritance — using code examples to demonstrate that aside from its JVM entry-point role, main behaves like any other static method.

Java Architect Handbook
Java Architect Handbook
Java Architect Handbook

The article begins with a real Alibaba second-round interview question: Can the main method be inherited in Java? It then systematically examines three related behaviors of the main method.

main Method Signature and JVM Requirements

The standard entry-point signature is:

public static void main(String[] args) {

}

Each modifier serves a purpose:

public : Highest access level so the JVM can invoke it.

static : Allows invocation without instantiating the class.

void : No return value needed on exit.

String[] args : Accepts command-line arguments; since JDK 1.5, varargs String... args is also valid.

Aside from this specific signature required by the JVM, any other main method is just an ordinary static method.

Can main Be Overloaded?

Yes. The article provides a working example:

public class Main {
    public static void main(String args) {
        System.out.println("hello world:" + args);
    }

    public static void main(String[] args) {
        main("test");
    }
}

The JVM only recognizes the String[] version as the entry point; the overloaded String version is callable like any other method.

Can main Be Called by Other Methods?

Yes. A recursive demonstration shows the entry-point main calling a helper main2, which in turn calls main again:

public class Main {
    private static int times = 3;

    public static void main2(String[] args) {
        times--;
        main(args);
    }

    public static void main(String[] args) {
        System.out.println("main方法执行:" + times);
        if (times <= 0) {
            System.exit(0);
        }
        main2(args);
    }
}

Output:

main方法执行:3
main方法执行:2
main方法执行:1
main方法执行:0

The example also warns about infinite loops if exit logic is missing.

Can main Be Inherited?

Yes. When a parent class defines main and a child class does not, running the child class executes the inherited main:

public class Main {
    public static void main(String[] args) {
        System.out.println("hello world");
    }
}
public class Main2 extends Main {}
// Running Main2 prints "hello world"

If the child class declares its own main, it hides the parent's version (static method hiding):

public class Main2 extends Main {
    public static void main(String[] args) {
        System.out.println("hello world Main2");
    }
}
// Running Main2 prints "hello world Main2"

Conclusion: Except for the JVM-mandated entry-point signature, main behaves exactly like any other public static method — it can be overloaded, invoked, and inherited (or hidden).

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaJVMInterview Questionsinheritancestatic methodsmain methodoverloading
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.