There are four types or flavors of nested classes in Java:
- Static nested class
- Inner class
- Local inner class
- Anonymous inner class
The distinctions among these four flavors are not evident at first sight, and it does not help matters that alternative terms are often substituted for them. To help clarify the confusion, we represent the four flavors of nested classes schematically in Figure 3-6:
To get you started, here is a question dealing with inner classes and interfaces.
Which of the following options can be a part of a correct inner class declaration
or a combined declaration and instance initialization ?
(Assume that SimpleInterface and ComplexInterface are interfaces.)
Select 2 options:
- private class C { }
- new SimpleInterface() { //valid code}
- new ComplexInterface(x) { //valid code}
- private final abstract class C { }
- new ComplexClass() implements SimpleInterface { }
Answer: a, b
Explanation:
c. You cannot pass parameters when you implement an interface by an
anonymous class.
d. A final class can never be abstract.
e. The
implements keyword is used only in a class definition and not during instantiation.