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.
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
Peter den Haan | peterdenhaan.com | quantum computing specialist, Objectivity Ltd
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.:
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???
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.
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
Peter den Haan | peterdenhaan.com | quantum computing specialist, Objectivity Ltd
incandescent light gives off an efficient form of heat. You must be THIS smart to ride this ride. Tiny ad:
Gift giving made easy with the permaculture playing cards