can we have a main() in an interface and different implementations for main() in the classes that implement this interface?

六眼飞鱼酱① 提交于 2019-12-05 02:37:56
UmNyobe

No you cannot, because main has to be static in order to be used as an entry point, and Interfaces dont allow the use of static, until Java 7.

Yes you can run a psvm in an interface, if you're working in Java 8. Because static methods are allowed in an interface starting from Java 8.

But of course, you cannot override the main method, since psvm is a static method.

With Java-8 you can have main method defined inside the interface, Below code will work in Java-8.

public interface TestInterfaces {
    public static void main(String[] a){    
        System.out.println("I am a static main method inside Inteface !!");
    }
}

There are two answers for your question.

  1. First of all, you can't have static methods in an Interface
  2. Yes you can overload main() method, but when you launch your class, only the public static void main(String args[]){} method will be treated as an entry point.

For example

public class Test {
    public static void main(String[] args) {
        System.out.println("entry point)");
    }

    public static void main(String arg1) {
        System.out.println("overload2");
    }

    public static void main(String arg1, String arg2) {
        System.out.println("overload3");
    }
}

When you launch the above class, the out will be "entry point"

I am not sure But You can have multiple main methods in multiple classes (not the same class). We start the program with the name of the class whose main method we want to run. So after that JVM will look for main method in that class only. So there should not be any problem.

I am not sure so please let me know if I am wrong.

You can create you own startup mechanism, but I am not sure why you would want to.

public class RunTests {
    public static void main(String... args) throws ClassNotFoundException {
        List<Class> classes = new ArrayList<Class>();
        try {
            for (String arg : args) {
                classes.add(Class.forName(arg));
            }
        } catch (ClassNotFoundException e) {
            if (classes.isEmpty())
                throw e;
        }
        String[] remainingArgs = Arrays.asList(args).subList(classes.size(), args.length).toArray(new String[0]);
        for(Class clazz: classes) {
            try {
                Test test = (Test) clazz.newInstance();
                test.main(remainingArgs);
                test.display();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

interface Test {
    public void main(String... args);
    public void display();
}

BTW: You don't have to have a main() method, e.g.

class NoMain {
    static {
       System.out.println("Hello World");
       System.exit(0);
    }
}

compiles and runs without error.

you declared a main(String[] args) as static in Testclass1,but in interface "test" it is non-static and interface not allow main(String[] args) as static .even if you override main(String[] args) in Testcalss1 as non-static it's not allowed, because main(String args) is already defined in Testclass1.so you cannot declare main(String[] args) in interface.And u done a one more mistaken is initializing the interface test as t.You cannot do that.

main() is static.so, we cant override static methods.Interfaces allows abstract methods methods & they will be implemented in subclasses.So, abstract methods never be static.finally I concluded that main() is not possible to execute in interfaces.

You can write static main method in Interface in java 8 but you can't override it as it's a static one.

I think you are missing something. Static methods (like the main method in Testclass1 and Testclass2) cannot override subclass methods.

Answer : Yes, we can provide different implementation of main() declared in an interface and classes implementing that interface by overriding method and can overload static main method if defined in an interface.

Some more information regarding interface changes in Java 8.

Prior to Java 8, it was not possible to DEFINE methods inside interface.

But there are changes made to Java 8 with respect to interface where one can DEFINE default and static method inside an interface. Hence below code will work fine without any issue.

public interface TestInterfaces {
    public static void main(String[] a){    
        System.out.println("I am a static main method inside Inteface !!");
    }
}

For information regarding this features, please refer below link: http://www.journaldev.com/2752/java-8-interface-changes-static-method-default-method

this is a compiler error. you cant override a non static interface a static method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!