0

Well any Java programmer knows that a Java Interface only allows declaration of methods. You cannot define the methods inside interfaces.

Yet if somebody like me wants you to define a method in interface and wants the program gets compiled and runs successfully, what would you do?

Well the answer is Inner Classes in Java. Through Inner classes, you can define a method in interfaces and run your programs.

Here's what you need to do:

public interface IMain{
        public class Main{
              public static void main(String [] args){
                   System.out.println("The main is defined inside an Interface". Don't believe me? Look at the source.. Geee..");
              }
        }
}

compile the program using javac.
run the program by typing java IMain$Main.
4
Contributors
3
Replies
5
Views
10 Years
Discussion Span
Last Post by Ezzaral
0

Well, perhaps you could do that, but there is no reason at all that you should do that. If you want to provide method implementations along with an interface definition you should be using an abstract class.

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.