Why we Define a Class Inside an Interface

How we can use a class defined inside an interface?
or Why we define a class inside an interface?

public interface abc
{
 static int i=0; void dd();
 class a1
 {
  a1()
  {
   int j;
   System.out.println("inside");
  };
  public static void main(String a1[])
  {
   System.out.println("in interfia");
  }
 }
}

A class is defined inside an interface to bind the interface to a TYPE.

A small but nonsense example:

interface employee{
    class Role{
          public String rollname;
          public int Role id;
          public Object person;
     }
    Role getRole();
    // other methods
}

In the above interface you are binding the Role type strongly to the employee interface(employee.Role).

One more reason is to have modifiable data within an interface(As you may know all fields in an interface are implicitly final). By declaring a class inside the interface you can have common MODIFIABLE data.

public interface abc{
    static int i=0; void dd();
    class a1{
 private int a1;
 int getA1(){
         return a1;
  }
 int setA1(int a){
 a1 = a;
 }
    }
   a1Instance a1Ins = new a1Instance();
}

I am not sure what you were trying to do with the main() in the interface.

Vinay.

Java Tips

See also

Do you have a Java Problem?
Ask It in The Java Forum

Java Books
Java Certification, Programming, JavaBean and Object Oriented Reference Books

Return to : Java Programming Hints and Tips

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site www.erpgreat.com is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of
www.erpgreat.com or the content authors.