Private Interface example

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vinothg
    New Member
    • Oct 2006
    • 21

    #1

    Private Interface example

    Just thought of posting this example about private interface which i learned from bruce eckel book "Thinking in Java"

    An interface can be nested within a class or another interface. If the interface is nested within a class it can be public or private. But if a interface is nested within a interface it should be public which is by rule.

    I was wondering what will be the use of private interface within a class. Then came up with this design.

    class librarian {

    private interface libraryRules{
    void takeHome();
    }

    public class book implements libraryRules{
    private book() {}
    public void takeHome(){
    System.out.prin tln("takeHome this book");
    }
    }
    public static libraryRules getPermission(l ibrarian ob1){
    libraryRules obj = ob1.new book();
    return obj;
    }

    public void gotPermission(l ibraryRules tmp) {
    tmp.takeHome();
    }
    }

    public class library{
    public static void main (String arg[]){
    librarian obj = new librarian();
    obj.gotPermissi on(librarian.ge tPermission(obj ));
    }
    }
Working...