Home » Java programming language

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.
Submitted by Preeti Jain, on July 18, 2019

The question is that "Can we override main() method in Java?"

  • No, we can't override the main() method in java.
  • First, we will understand what is 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.
  • Now, the question is to raise why main() method can't override so we will see the answer of this question main() method is not overridden 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.
  • 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 (Case1): We will see, in a java program to demonstrate 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

E:\Programs>javac WithoutOverridingMain.java

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

Example (Case2) : We will see in a java program to demonstrate 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

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

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.