this is how we can override main function in java….
public class animaltest
{
public static void main(String[] args)
{
horse h = new horse();
h.eat();
}
}
public class inheritmain extends animaltest
{
public static void main(String[] args)
{
System.out.print("main overrided");
}
}
but what is the benefit of overriding main??
staticmethods do not override: they are shadowed. There are two different independent static methods in that case, namelyanimaltest.mainandinheritmain.main. (See Can we override static method in Java?)The “advantage” — if any 😉 — is that the program can be started/launched from either class as both classes implement the main method:
Happy coding.