I know that main() can be overloaded in a class with the compiler always taking the one with String[] args as arguments as the main method from where the execution starts. But is it possible to declare the same
main(String args[]) in an interface and implement it in different classes differently?
For example,
package test;
interface test
{
public void main(String args[]);
public void display();
}
package test;
class Testclass1 implements test
{
public void display()
{
System.out.println("hello");
}
public static void main(String[] args)
{
test t;
t.display();
}
}
package temp;
import test.*;
abstract class Testclass2 implements test
{
public static void main(String args[])
{
System.out.println("TESTING");
}
}
No you cannot, because
mainhas to be static in order to be used as an entry point, and Interfaces dont allow the use of static, untilJava 7.Yes you can run a
psvmin an interface, if you’re working inJava 8. Because static methods are allowed in an interface starting from Java 8.But of course, you cannot override the
mainmethod, sincepsvmis a static method.