Fundamentals 6 min read

Can Java’s main Method Be Inherited? Exploring Overloading and Calls

This article explains the special nature of Java's main method, its modifiers, how it can be overloaded, invoked from other methods, and even inherited by subclasses, illustrating each point with clear code examples.

Java High-Performance Architecture
Java High-Performance Architecture
Java High-Performance Architecture
Can Java’s main Method Be Inherited? Exploring Overloading and Calls

During a recent interview discussion, a candidate was asked whether the main method in Java can be inherited. The article uses this question to explore the characteristics of the main method.

Definition of the main method

The standard signature is:

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

Each keyword has a specific purpose:

public : the JVM must be able to access the method, so it must be publicly visible.

static : the method is called by the JVM without creating an instance of the class.

void : the entry point does not return a value.

String[] : an array of strings supplies command‑line arguments; since Java 1.5, the var‑args form String... args is also allowed.

Can the main method be overloaded?

Yes. For example:

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

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

Both methods compile and run; only the signature defined by the JVM ( public static void main(String[] args)) is used as the program entry point.

Can other methods call main?

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 method execution:" + times);
        if (times <= 0) {
            System.exit(0);
        }
        main2(args);
    }
}

Running this code prints the decreasing counter and demonstrates that the entry‑point main can be invoked like any other static method, provided the program termination is handled correctly.

Can the main method be inherited?

If a superclass defines a public static void main(String[] args) method, a subclass inherits it:

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

public class Main2 extends Main {
}

Executing Main2 prints hello world, showing that the method is inherited. Subclasses can also hide the inherited version by defining their own main method:

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

Running Main2 now prints hello world Main2, demonstrating method hiding.

In summary, apart from being the special entry point required by the JVM, the main method behaves like any other static method: it can be overloaded, called from other methods, and inherited or hidden by subclasses.

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.

Inheritancestaticmain methodMethod Overloading
Java High-Performance Architecture
Written by

Java High-Performance Architecture

Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.

0 followers
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.