Inner classes Tutorial Java Questions Answers


Inner classes Tutorial Java

1. What is inner class in Java? Explain with example?
2. What is nested class in Java? Explain clearly?

A class declared within another class is known as inner class or nested class.

public class Demo	                        // outer class
{
 class Test		                        // inner class or nested class
 {

 }
}

In the above code, Demo is known as outer class and Test is known as inner class. Test is declared within the body of Demo class. Demo is also known as top-level class. Test is not subclass; it is inner class. To access Test, it is necessary to go through Demo class. Demo class, being top-level class, can have an instance as in normal way. But, an instance of Test should be created within the context of Demo object. This will be known in the example.

Demo can be a direct member of a package but not Test. Inner classes were introduced with JDK 1.1.

Read for inner classes introduction, 4 types of inner classes with screenshot at Inner classes Introduction

3. Name other languages where nested classes concept exists?

Java introduced nested class with JDK 1.1. Then afterwards, nested classes were also introduced in the languages like Ruby, Visual Basic, .NET, Python and C# etc.

4. What are the advantages of inner classes in Java?
5. Why we use inner classes in Java?

  1. Let us take an example of House and Furniture. Furniture is a part of House and without house, Furniture cannot exist. We can declare Furniture within House. This can be modelled nicely with the concept of inner classes. Declare class House as outer class and class Furniture as inner class.
  2. Access specifiers exist in Java to impose restrictions for accessibiliyt. Inner classes give more restrictions on accessibility.
  3. Package structure can be better organized through nested classes (instead of flat packages).
  4. If properly organized, inner classes allows to create a structural hierarchy.
  5. Inner classes, especially anonymous inner classes, can be used in GUI to close the frame window.

6. What are the disadvantages of inner classes?

  1. For a novice, inner classes organization, rules and also testing becomes difficult to understand.
  2. Java tools like Eclipse and VisualAge may not support the implicit creation of inner classes like top-level classes.

7. How many types of inner classes are there in Java?

There are four types of inner classes.

  1. Static member class: Here, the inner class is static.
  2. Member class: The inner class is declared with in an outer class (here, inner class is not static).
  3. Local class: Inner class is declared within the method of outer class.
  4. Anonymous class: Here, the inner class does not have a name.

All the 4 types of inner classes examples and restrictions of usage with screenshots are available at Inner classes Introduction and 4 Types

8. What is static inner class or static member class?
9. What is static nested class?
10. Can we declare an inner class as static?

Here, the inner class is static.

public class Demo	                      // outer class
{
  static class Test		              // static inner class 
  {

  }
}

The small change in this example than previous (without static) is here the inner class is declared static. The inner class can have static and non-static members like the outer class. But the restriction is "the static inner class cannot access outer class non-static members".

It is more explained with screenshot at Static Nested Classes

11. Can we have interface inside another interface?
12. Can we have nested interfaces?
13. Can we have inner interfaces?

Yes, it is possible. An interface can be declared within the body of another interface. The rule is an interface declared within another interface must be public only and is implicitly static. That is, inner interface should be public.

Nested interfaces are useful to group all the interfaces with some common functionality. It is to group the interfaces having some common functionality (purpose).

public interface Vehicle
{
  public interface Wheels {   }
  public interface Steering {   }
  public interface Engine {   }
}

Here, exists three nested interface.

How to write nested interfaces and how they are to be implemented is explained clearly in Interface inside Interface

14. Can we have interface inside a class in Java?
15. Can we have a nested interface in a class?
16. Is it possible to have interface inside a class?

Yes, it is possible. Nested interface within a class is a tieing mechanism between an interface and a class – another type of organizing the classes and interfaces.

A Program with code, explanation and output screen is available at Nested Interface within Class

17. Can we have a class inside an interface?
18. Can we have a nested class in interface in a class?
19. Is it possible to have a class in interface?

Yes, definitely can exist. The advantage is the class is tied with the interface. The other class cannot use nested class without using interface.

It is more explained with example and screenshot at Class inside Interface

20. What is anonymous inner class and where it is mostly used?

Anonymous inner class does not have a name. Anonymous inner classes are used mostly in event handling of GUI components. The best example is closing the frame.

Pass your suggestions and comments to improve the quality of this "Inner classes Tutorial Java".

Leave a Comment

Your email address will not be published.