User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 384,018 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,233 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Aug 31st, 2007
Views: 3,877
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:
java Syntax | 2 stars
  1. public interface IMain{
  2. public class Main{
  3. public static void main(String [] args){
  4. System.out.println("The main is defined inside an Interface". Don't believe me? Look at the source.. Geee..");
  5. }
  6. }
  7. }
  8.  
  9. compile the program using javac.
  10. run the program by typing java IMain$Main.
  11.  
Comments (Newest First)
Ezzaral | Posting Maven | Feb 11th, 2008
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.
Allen Chen | Newbie Poster | Jan 5th, 2008
Think about in any class?
manix413 | Newbie Poster | Jan 2nd, 2008
jjjjj
Post Comment

Only community members can submit or comment on code snippets. You must register or log in to contribute.

DaniWeb Marketplace (Sponsored Links)
All times are GMT -4. The time now is 4:45 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC