Java Inner Class is defined inside the body of another class. Java inner class can be declared private, public, protected, or with default access whereas an outer class can have only public or default access. As with instance methods and variables, an inner class is associated with an instance of its enclosing class and has direct access to that object's methods and fields. Also, because an inner class is associated with an instance, it cannot define any static members itself.
1. Inner Classes represent a special type of relationship that is it can access all the data members and methods of outer class including private.
2. Inner Classes are used to develop more readable and maintainable code because it logically group classes and interfaces in one place only.
3. Code Optimization requires less code to write.
Nested Inner Class : It can access any private instance variable of the outer class. Like any other instance variable, we can have access modifier private, protected, public and default modifier. Like class, an interface can also be nested and can have access specifiers.
Static Inner Class : Static Inner Class type of inner class is also defined at a class's member level but is declared static. This means it can be instantiated without an instance of the outer class. It cannot access non-static members of the outer class.
Anonymous Inner Class : This is a special type of local inner class that does not have a name.It is typically used for implementing interfaces or extending classes on the fly.
Method Local Inner Class : Method Local Inner Class is the most common type of inner class. It is defined at the member level of a class, just like methods or variables. It can access all members of the outer class (including private members) and can be instantiated only with an instance of the outer class.
It is a way of logically grouping classes that are only used in one place : If a class is useful to only one other class, then it is logical to embed it in that class and keep the two together. Nesting such "helper classes" makes their package more streamlined.
It increases Encapsulation : Consider two top-level classes, A and B, where B needs access to members of A that would otherwise be declared private. By hiding class B within class A, A's members can be declared private and B can access them. In addition, B itself can be hidden from the outside world.
It can lead to more readable and maintainable code: Nesting small classes within top-level classes places the code closer to where it is used.
Encapsulation : Inner classes can access private variables and methods of the outer class. This helps to achieve encapsulation and improves code readability.
Code Organization : Inner classes allow you to group related code together in one place. This makes your code easier to understand and maintain.
Better Access Control : Inner classes can be declared as private, which means that they can only be accessed within the outer class. This provides better access control and improves code security.
Callbacks : Inner classes are often used for implementing callbacks in event-driven programming. They provide a convenient way to define and implement a callback function within the context of the outer class.
Polymorphism : Inner classes can be used to implement polymorphism. You can define a class hierarchy within the outer class and then create objects of the inner classes that implement the different subclasses.
Reduced Code Complexity : Inner classes can reduce the complexity of your code by encapsulating complex logic and data structures within the context of the outer class.