Nested class inside an interface
Asked Answered
G

3

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 .

Gerhan answered 1/2, 2012 at 15:11 Comment(2)
possible duplicate: #595289Pester
possible duplicate of inner class within InterfacePacificia
P
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

Pester answered 1/2, 2012 at 15:19 Comment(3)
Indeed.. Here I am using it to embed the Null object implementation of an interface, so I don't need to create a class file for it.Grampus
Clicking on "This" page link led me to a malware deceptive installation page.Tahoe
Link has been updated, thanks for the heads up @Blessed GeekPester
O
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).

Octameter answered 1/2, 2012 at 15:16 Comment(0)
P
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

Pester answered 1/2, 2012 at 15:19 Comment(3)
Indeed.. Here I am using it to embed the Null object implementation of an interface, so I don't need to create a class file for it.Grampus
Clicking on "This" page link led me to a malware deceptive installation page.Tahoe
Link has been updated, thanks for the heads up @Blessed GeekPester
T
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.

Tahoe answered 17/10, 2022 at 10:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.