• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Interface inside the class

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
reply
    Bookmark Topic Watch Topic
  • New Topic