Java interview questions with answers
Home Java Interview Questions Revision Notes Resources Interview Tips Interview Samples

Valid HTML 4.01 Transitional

Java Interview Questions & Answers

Part 8 - Inner Classes

What are Inner Classes?
Inner classes are classes which are defined inside another class.

What are Nested Classes?
Static Inner classes are called sometimes referred as nested classes because these classes can exist without any relationship with the containing class.

What is the super class of all Inner Classes?
Inner class is just a concept and can be applied to any class, hence there is no common super class for inner classes.

What are the disadvantages of Inner classes?
1. Inner classes are not reusable hence defeats one of the fundamental feature of Java.
2. Highly confusing and difficult syntax which leads poor code maintainability.

Name the different types of Inner Classes?
The following are the different types of Inner classes:

  • Regular Inner Class
  • Method Local Inner Class
  • Static Inner Class
  • Anonymous Inner Class       
What are Regular Inner Classes?
A Regular inner class is declared inside the curly braces of another class outside any method or other code block. This is the simplest form of inner classes.

Can a regular inner class access a private member of the enclosing class?
Yes. Since inner classes are treated as a member of the outer class they can access private members of the outer class.

How will you instantiate a regular inner class from outside the enclosing class?
Outer out=new Outer();
Outer.Inner in=out.new Inner();

What is the impact of marking an Inner Class as abstract?


Explain how "this" works with respect to inner classes?
Within the inner classes "this" refers to the current object but if an inner class needs to access an current outer class object, then it should outer class name along with this. The below is an example for the same.
<Program needs to be given>

What are Local Inner Classes or Method Local Inner Classes?
A method-local inner class is defined within a method of the enclosing class.

What are the constraints on Method Local Inner Classes?
The following are the restrictions for Method Inner Classes:
  • Method Local Inner classes cannot acccess local variables but can access final variables.
  • Only abstract and final modifiers can be applied to Method Local Inner classes
  • Method Local Inner classes can be instantiated only within the method in which it is contained that too after the class definition.
What are Anonymous Inner Classes? Name the various forms of Anonymous Inner Classes.
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. The following are the different forms of inner classes:
  • Anonymous subclass(i.e. extends a class)
  • Anonymous implementer (i.e. implements an interface)
  • Argument-Defined Anonymous Inner Classes
How many classes can an Anonymous Inner classes inherit from?
One.

How many Interfaces can an Anonymous Inner classes implement?
One. Normal classes and other inner classes can implement more than one interface whereas anonymous inner classes can either implement a single interface or extend a single class.

In which scenarios are Anonymous Inner classes frequently used?

What are Static Inner Classes?
Static Inner Classes are inner classes which marked with a static modifier. These classes need not have any relationship with the outer class. These can be instantiated even without the existence of the outer class object.

Can you instantiate the static Inner Class without the existence of the outer class object? If Yes, Write a sample statement.
Yes. It can be instantiated as follows by referencing the Outer class.

Outer.Inner in = new Outer.Inner();

What are the constraints on Static Inner Classes?
  • It cannot access non-static members of the outer class.
  • It cannot use this reference to the outer class.
On many class files are produced for source file having one Outer class and one Inner class?
Two class files will be produced as follows:
Outer.class
Outer$Inner.class




Pls send your questions, suggestions and feedback to j2eeguide@yahoo.com

© Copyright JGuide 2006, All Rights Reserved.