Java - Interview Questions and Answers on Inner / Nested Classes

Q1.  What are different types of inner classes ?

Ans. Simple Inner Class, Local Inner Class, Anonymous Inner Class , Static Nested Inner Class.

Q2.  Which access specifier can be used with Class ?

Ans. For top level class we can only use "public" and "default". We can use private with inner class.

Q3.  Difference between nested and inner classes ?

Ans. Inner classes are non static nested classes.

Q4.  What is a nested interface ?

Ans. Any interface declared inside a class or an interface. It is static by default.

Q5.  What is the benefit of inner / nested classes ?

Ans. You can put related classes together as a single logical group.

Nested classes can access all class members of the enclosing class, which might be useful in certain cases.

Nested classes are sometimes useful for specific purposes. For example, anonymous inner classes are useful for writing simpler event-handling code with AWT/Swing.

Q6.  Explain Static nested Classes ?

Ans. The accessibility (public, protected, etc.) of the static nested class is defined by the outer class.

A static nested class is not an inner class, it's a top-level nested class.

The name of the static nested class is expressed with OuterClassName.NestedClassName syntax.

When you define an inner nested class (or interface) inside an interface, the nested class is declared implicitly public and static. 

Static nested classes can be declared abstract or final.

Static nested classes can extend another class or it can be used as a base class.

Static nested classes can have static members. 

Static nested classes can access the members of the outer class (only static members, obviously).

The outer class can also access the members (even private members) of the nested class through an object of nested class. If you don’t declare an instance of the nested class, the outer class cannot access nested class elements directly.

Q7.  Explain Inner Classes ?

Ans. The accessibility (public, protected, etc.) of the inner class is defined by the outer class. 

Just like top-level classes, an inner class can extend a class or can implement interfaces. Similarly, an inner class can be extended by other classes, and an inner interface can be implemented or extended by other classes or interfaces.

An inner class can be declared final or abstract.

Inner classes can have inner classes, but you’ll have a hard time reading or understanding such complex nesting of classes.

Q8.  Explain Method Local Inner Classes ?

Ans. You can create a non-static local class inside a body of code. Interfaces cannot have local  classes, and you cannot create local interfaces.

Local classes are accessible only from the body of the code in which the class is defined. The local classes are completely inaccessible outside the body of the code in which the class is defined.

You can extend a class or implement interfaces while defining a local class.

A local class can access all the variables available in the body of the code in which it is defined. You can pass only final variables to a local inner class.

Q9.  Explain about anonymous inner classes ?

Ans. Anonymous classes are defined in the new expression itself, so you cannot create multiple objects of an anonymous class.

You cannot explicitly extend a class or explicitly implement interfaces when defining an anonymous class.

An anonymous inner class is always created as part of a statement; don't forget to close the statement after the class definition with a curly brace. This is a rare case in Java, a curly brace followed by a semicolon.

Anonymous inner classes have no name, and their type must be either a subclass of the named type or an implementer of the named interface