Inner Classes in Java With Examples

The inner class in Java is the class that is declared within another class or within another interface. It is also known as nested class. As classes and interfaces are logically grouped in a single place by using the inner classes, it makes the program easily maintainable and more readable. The inner class can access each member of the outer class, even the private methods, and data members.

Syntax:

Benefits of Java inner classes

Some of the benefits of using inner classes in Java are:

  • The inner class can access all the members of the outer class, even the private ones as it represents a greater type of relationship.
  • The classes and interfaces are logically grouped in a single place by using the inner classes so it makes the code easily maintainable and readable.
  • The use of inner class decreases the need to write more code.

The dissimilarity between nested class and inner class in Java

The inner class is the part of the nested class that is non-static.

Types of nested classes

The nested class is divided into two types: Static nested class and non-static nested class. The non-static classes or the inner classes are further divided into three types.

1. Static nested class

A static nested class is the type of nested class that is created within a class. The static nested class can only access static data members and method, not the non-static. It can also access the private data members of the outer class. The outer class name can only access the static nested class.

2. Non-static nested class

The three types of non-static nested class are explained below:

  • Member inner class:

A member inner class is the type of inner class that is created inside a class but outside a method.

  • Internal working of Java member inner class

In the case of the member inner class, the program created two class files. The class file is named Outer$Inner. The instance of the outer class must be created by the programmer if they want to instantiate the inner class. In this situation, an instance of the inner class is created inside the instance of the outer class.

  • The internal code generated by the compiler

In case of the member inner class, the program created a class file named Outer$Inner. The member inner class can access all the data members of the outer class as it has the reference of the outer class.

3. Anonymous inner class

An anonymous inner class is the type of inner class that implements an interface or extends a class. The Java compiler decides the name of the anonymous inner class as it does not have any name. It is used when the programmer has to perform method overriding of a class or an interface.

There are two methods to create an anonymous inner class and they are:

  • Through class
  • Through interface

Local inner class:

A local inner class is the type of inner class that is created inside another class and also within a method. The class inside the method must be instantiated if the programmer wants to call on the methods of the local inner class.

Rules for Java local inner class

  • The local inner class cannot be called on from outside the method.
  • The local inner class could only access the final local variable till JDK 1.7 but after JDK 1.8 it can also access the non-final local variable.

# Nested interface in java

In Java, a nested interface can be defined as an interface that is declared inside another interface on a class. The main function of the nested interface is to group related interfaces to make the code easily maintainable. The outer interface or class can only refer to the nested interface and it cannot be accessed directly.

Main features of nested interfaces

The following are some of the major features of the nested interfaces:

  • If a nested interface is declared within another interface, it should be public. However, if it is declared inside a class, the nested interface can include any access modifier.
  • The nested interfaces are totally declared as static.

Can a class be defined inside an interface?

When the programmer defines a class within an interface, a static nested class is created by the program.

Leave a Reply

Your email address will not be published. Required fields are marked *