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

I know that main () can be overloaded in the class when the compiler always takes a value with String[] args as arguments to the main method from which 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 instance,

 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"); } } 
+6
source share
11 answers

No, you cannot, because main must be static in order to be used as an entry point, and interfaces do not allow the use of static ones.

+12
source

With Java-8, you can have a main method defined inside an 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
source

There are two answers to your question.

  • First of all, you cannot have static methods in Interface
  • Yes, you can overload the main() method, but when you run your class, public static void main(String args[]){} will be considered an entry point.

for instance

 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 starting the above class, the output will be " entry point "

+2
source

I'm not sure. But you can have several basic methods in several classes (not one class) . We run the program with the name of the class whose main method we want to run. Therefore, after this, the JVM will look for the main method only in this class. Therefore, there should be no problems.

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

0
source

You can create your own startup mechanism, but I'm not sure why you want it.

 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 do not need to have a main () method, for example

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

compiles and runs without errors.

0
source

you declared the main (String [] args) as static in Testclass1, but in the test interface it is non-stationary, and the interface does not allow main (String [] args) as static. Even if you redefine main (String [] args) in Testcalss1 as non-static, it is not allowed because main (String args) is already defined in Testclass1.so you cannot declare main (String [] args) in the interface. And you did another erroneous initialization test of the interface like that. You cannot do this.

0
source

main () - static.so, we cannot override static methods. Interfaces accept methods of abstract methods, and they will be implemented in subclasses. Therefore, abstract methods are never static. Finally, I came to the conclusion that main () cannot be executed on interfaces.

0
source

You can write a static main method in an interface in java 8, but you cannot override it as static.

0
source

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

-1
source

Answer: Yes, we can provide a different implementation of main () declared in the interface, and classes that implement this interface by overriding the method and can overload the static main method if it is defined in the interface.

Learn more about interface changes in Java 8.

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

But there are changes made in Java 8 with respect to the interface, where you can set the default DEFINE and the static method inside the interface. Therefore, below code will work fine without any problems.

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

See below for information on these features: http://www.journaldev.com/2752/java-8-interface-changes-static-method-default-method

-1
source

This is a compiler error. you cannot override the non-stationary interface static method

-2
source

Source: https://habr.com/ru/post/910690/


All Articles