• 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

Why embed class in interface??

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From a higher-level OO viewpoint, why did the Java language designers decide to allow a class to be contained within an interface?? what purpose does it serve??

interface A {
class B {
void D() { System.out.println("interface A - D"); }
void E() { System.out.println("interface A - E"); }
}
}

To me, it just seems like a corruption of the design of the interface. I am curious if I am missing something or if it is like the tail appendage in the human body.
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Although I've not come across a good use case yet for a (named) class inside an interface, I do occasionally use an anonymous class to provide a null implementation:Client code will often use this as a default value for a Foo:Using this null object pattern where you would otherwise use the null value tends to simplify your code, and also make it less prone to bugs (especially NullPointerExceptions if you forgot to stick in a null check somewhere).

- Peter
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oooh, What a great answer!, I'll be sure to use that myself.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that the general approach in Java for nested/inner types has been, they allow you to declare a class just about anywhere you could declare a variable, unless there's a particular reason you shouldn't be able to. They don't prevent you from doing it just because there's no particular positive reason to allow the usage. So nesting a class in an interface is allowed - why not? - even though it seems to be seldom useful.

One possible use for a nested class here is to define a new Exception type which is closely related to the interface, and generally useless to anyone not using hte interface. E.g.:
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you do have a class nested within an interface, it will be abstract since all interface members are implicitly abstract.

Keeping that in mind, the nested class will have to be sub-classed. So if a concrete class implements the said interface....will it automatically extend tha nested class???

or how does it work??
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Murtuza,


If you do have a class nested within an interface, it will be abstract since all interface members are implicitly abstract.



A class defined inside the Interface is not a member method of the class. The statement is true only for interface methods, not for any declaration or interface fields (for e.g. interface fields are not abstract).

Here is an example:



You could call the methods as
too.

How you use this feature at your work, depends on the design of the system.
 
Peter den Haan
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Murtuza Akhtari:
If you do have a class nested within an interface, it will be abstract since all interface members are implicitly abstract.

Then how do you explain away the Foo.NULL example I gave above?

- Peter
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic