How to overload and override main method in Java - GeeksforGeeks

05 Apr, 2019
Method Overloading can be defined as a feature in which a class can have more than one method having the same name if and only if they differ by number of parameters or the type of parameters or both, then they may or may not have same return type. Method overloading is one of the ways that java support Polymorphism.Yes, We can overload the main method in java but JVM only calls the original main method, it will never call our overloaded main method.Below example illustrates the overloading of main() in javaExample 1:
// Java program to demonstrate// Overloading of main()  public class GFG {      // Overloaded main method 1    // According to us this overloaded method    // Should be executed when int value is passed    public static void main(int args)    {        System.out.println("main() overloaded"                           + " method 1 Executing");    }      // Overloaded main method 2    // According to us this overloaded method    // Should be executed when character is passed    public static void main(char args)    {        System.out.println("main() overloaded"                           + " method 2 Executing");    }      // Overloaded main method 3    // According to us this overloaded method    // Should be executed when double value is passed    public static void main(Double[] args)    {        System.out.println("main() overloaded"                           + " method 3 Executing");    }      // Original main()    public static void main(String[] args)    {        System.out.println("Original main()"                           + " Executing");    }}
Output:
As from above example, it is clear that every time original main method executes but not the overloaded methods because JVM only executes the original main method by default but not the overloaded one.So, to execute overloaded methods of main, we must call them from the original main method.Example 2:In this example, we will execute all the Overloads of the main method one by one
// Java program to demonstrate// Overloading of main()  public class GFG {      // Overloaded main method 1    public static void main(boolean args)    {        if (args) {            System.out.println("main() overloaded"                               + " method 1 Executing");            System.out.println(args);              // Calling overloaded main method 2            GFG.main("Geeks", "For Geeks");        }    }      // Overloaded main method 2    public static void main(String a, String b)    {        System.out.println("main() overloaded"                           + " method 2 Executing");        System.out.println(a + " " + b);    }      // Overloaded main method 3    public static void main(int args)    {        System.out.println("main() overloaded"                           + " method 3 Executing");        System.out.println(args);    }      // Original main()    public static void main(String[] args)    {        System.out.println("Original main()"                           + " Executing");        System.out.println("Hello");          // Calling overloads of the main method          // Calling overloaded main method 1        GFG.main(true);          // Calling overloaded main method 3        GFG.main(987654);    }}
Output:
Original main() Executing
Hello
main() overloaded method 1 Executing
true
main() overloaded method 2 Executing
Geeks For Geeks
main() overloaded method 3 Executing
987654

Whenever we do inheritance in java then if a method in subclass has the same name and type signature as a method in its parent class or superclass, then it is said that the method in subclass is overriding the method of parent class. Method overriding is one of the way that java supports run time Polymorphism.No, we cannot override main method of java because a static method cannot be overridden.The static method in java is associated with class whereas the non-static method is associated with an object. Static belongs to the class area, static methods don’t need an object to be called. Static methods can be called directly by using the classname ( classname.static_method_name() ).So, whenever we try to execute the derived class static method, it will automatically execute the base class static method.Therefore, it is not possible to override the main method in java.To know more about Overriding static method in java Visit here
Java-Overloading
java-overriding
main
Picked
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Constructors in Java
Exceptions in Java
Stream In Java
Functional Interfaces in Java
Different ways of Reading a text file in Java
Internal Working of HashMap in Java
Iterators in Java
Java Programming Examples
Strings in Java
Comparator Interface in Java with Examples