6

The java compiler allows me write a Class definition inside an Interface . Are there any specific uses of this ?

interface ClassInterface {

  void returnSomething();
  int x = 10;

  class SomeClass {
    private int y;

    private void classDoingSomething() {         
    }
  }
}

Please explain .

AllTooSir
  • 48,828
  • 16
  • 130
  • 164
  • 1
    possible duplicate: http://stackoverflow.com/questions/594789/practical-side-of-the-ability-to-define-a-class-within-an-interface-in-java – Korhan Ozturk Feb 01 '12 at 15:16
  • 1
    possible duplicate of [inner class within Interface](http://stackoverflow.com/questions/2400828/inner-class-within-interface) – aioobe Feb 01 '12 at 15:16

3 Answers3

4

The uses are the same that a class nested in another class: it allows scoping the class to the interface. You could imagine something like this:

public interface Switch {

    public enum Status {
        ON, OFF;
    }

    void doSwitch();
    Status getStatus();
}

It avoids defining a top-level class named SwitchStatus (because Status could be a too generic name).

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
2

Yes Java allows you to define an inner class inside an interface One use I can think of is tightly binding a certain type (defined by the class) to an interface and perhaps limit access only to the interface methods. There's an example of such use on here

Korhan Ozturk
  • 11,148
  • 6
  • 36
  • 49
0

I use that pattern to embed multiple tiny concrete implementations that share a large amount of commonality. It's easier to manage 25 tiny extensions of the same abstract class within the same file. Using like in C# namespace enclosure.

/**
 * Dao 道 = access/way/avenue
 * Bao 包 = bundle/package/
 */
public interface DaoBao {
  public abstract class <E extends BlessedEntity> BlessedDaoWager<E,T> {
    private JdbcTemplate jtmpl;
    public void setDatasource(Datasource ds) {
      this.jtmpl = new JdbcTemplate(ds);
    }
    public E find(BlessedKey key) {
      blah .. blah .. blah ...        
    }
    public List<E> list(Date from, Date to) {
      blah .. blah .. blah ...
    }
    public boolean remove(BlessedKey key) {
      blah .. blah .. blah ...
    }
    public T getKey(E ent) {
      return ent.getId();
    }
  }

  public class BlessedEmployeeDao
    extends BlessedDaoWager<BlessedEmployeeEntity, Long> {
    public Long getKey(BlessedEmployeeEntity ent) {
      return ent.getCucurucucu();
    }
  }
  public class BlessedSalaryDao
    extends BlessedDaoWager<BlessedSalaryEntity, BlessedEmployeeEntity> {
    public BlessedEmployeeEntity getKey(BlessedSalaryEntity ent) {
      return ent.getEmployeeId();
    }
  }
  public class BlessedHoursDao
    extends BlessedDaoWager<BlessedHoursEntity, BlessedEmployeeEntity> {
    public BlessedEmployeeEntity getKey(BlessedSalaryEntity ent) {
      return ent.getEID();
    }
  }
  public class BlessedGooDao
    extends BlessedDaoWager<BlessedGooEntity, String> {
      public String getKey( ent) {
      return ent.getName();
    }
  }
  public class BlessedHowDao extends BlessedDaoWager<BlessedEntity, Long> {}
  public class BlessedNowDao extends BlessedDaoWager<BlessedEntity, Date> {}

}

There are those who say, what if someone inadvertently implemented the interface. I would say those are inadvertent programmers looking for ways to prevent their inadvertent programming habits.

Blessed Geek
  • 21,058
  • 23
  • 106
  • 176