• Post Reply Bookmark Topic Watch Topic
  • New Topic

Interface inside the class  RSS feed

 
Anupam Mittal
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Can somebody explain me in what situations we should declare a interface inside the class. It would be great if somebody can give one example.

Thanks in advance.

Anupam
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've used a nested interface in a class when I wanted to implement the Command/Strategy (always get those two confused) pattern within a class. The interface didn't seem to make much sense outside the scope of the class (or maybe I was too lazy to create another file). Anyway it certainly didn't have to be done that way.
 
Dana Bothner-By
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you nest one type inside another (class inside class, interface inside class, etc...) you're indicating that the nested type is subordinate to the enclosing type. That is to say, the enclosing type represents some fairly significant concept (that's why it is defined as such) while the nested type is a dependent "helper" to the enclosing type. One implication of this nesting is that you can't use/resue the nested class without involving the enclosing type. If you can think of a context where the nested type is needed but the enclosing type is irrelevant, then the nesting may be a mistake.

Another consequence of some nested definitions is that the inner object has an implicit reference to its parent object, but that's not necessarily going to happen with a nested interface -- it depends on where classes that implement the interface are defined.
[ August 25, 2006: Message edited by: Dana Bothner-By ]
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Dana Bothner-By:
... Another consequence of some nested definitions is that the inner object has an implicit reference to its parent object, but that's not necessarily going to happen with a nested interface...

Note that when interfaces are declared as members of classes, they are implicitly static. As such, they do not require an instance of the enclosing class and cannot reference non-static members of the enclosing class.
 
Dana Bothner-By
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by marc weber:

Note that when interfaces are declared as members of classes, they are implicitly static. As such, they do not require an instance of the enclosing class and cannot reference non-static members of the enclosing class.


Indeed, but then all the action will be in the class that implements the interface, and it may well be nested in the same class:

[ August 28, 2006: Message edited by: Dana Bothner-By ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!