can we have a main() in an interface and different

2019-04-26 03:41发布

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");
    }
}

11条回答
迷人小祖宗
2楼-- · 2019-04-26 04:18

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

查看更多
疯言疯语
3楼-- · 2019-04-26 04:21

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

查看更多
叼着烟拽天下
4楼-- · 2019-04-26 04:25

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.

查看更多
倾城 Initia
5楼-- · 2019-04-26 04:27

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

查看更多
再贱就再见
6楼-- · 2019-04-26 04:29

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 !!");
    }
}
查看更多
迷人小祖宗
7楼-- · 2019-04-26 04:29

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.

查看更多
登录 后发表回答