Can we override main() method in Java?

Here, we are going to learn about the main() method – can we override the main() method in java? We are explaining the concept of main() method overriding using examples. By Preeti Jain Last updated : January 02, 2024

Overriding

Overriding is what method signature will be the same in parent and child class and method body will be different in parent and child class.

Can we override main() method in Java?

No, we can't override the main() method in Java.

Why main() method can't override?

Because it is static and we can't override static methods or in other words static methods cannot be overridden.

The static method is a class method, it does not need an object instantiation so we can call static methods directly with the class name.

If we try to execute child class static method so it will indirectly parent class static methods will execute so, in that case, there is no sense of overriding and overwhelming the concept of inheritance too.

Explanation with an example

Let suppose if we keep static main() method in parent class and the same method override in child class and if we call child class main() method than by default parent class method will be called so there is no sense of overriding of static methods that's why main() method is not overridable because it is static.

The static method is a class method so the scope of the method within the same class itself that's why the overriding concept is not applicable for class methods or in other words static methods.

The overriding concept is applicable for instance methods.

Example 1: main() method without overriding

class WithoutOverridingMain {
    public static void main(String[] args) {
        System.out.println("main() method can't override");
        System.out.println("because this method is static");
    }
}

Output

The output of the above example is:

E:\Programs>javac WithoutOverridingMain.java

E:\Programs>java WithoutOverridingMain
main() method can't override because this method is static

Example 2: main() method with overriding

Note: It is not an overriding but seems to be overridden.

class Parent {
  // Parent class main() method
  public static void main(String[] args) {
    // Display a message for the user
    System.out.println("We are in Parent class main() method");
  }
}

class Child extends Parent {
  /*  Overriding parent class main() method that's is not possible
It looks like overriding but it is just another main method 
with same signature of parent class
*/
  public static void main(String[] args) {
    //Display a message for the user. 
    System.out.println("We are in Child class main() method");
  }
}

class Main {
  public static void main(String[] args) {
    // creating an object of Parent class
    Parent p = new Parent();

    // Calling Parent class method
    p.main(new String[0]);

    // Creating Child class object
    Child c = new Child();

    // Call Child class method
    c.main(new String[0]);
  }
}

Output

The output of the above example is:

E:\Programs>javac Main.java

E:\Programs>java Main
We are in Parent class main() method
We are in Child class main() method

Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.