Thursday, 5 November 2015

Nested Classes in Java

Nested Classes
Java inner class or nested class is the class declared inside another class or interface, known as outer (top level class).

class OuterClass {
      class InnerClass{ 

Why Nested Classes?
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.

To increases encapsulation:
Nested classes represent a special type of relationship that is it can access all the members (data members and methods) of outer class including private.

More readable and maintainable code.

Two categories Nested Classes:
Static Nested class: Nested class which declared as static.

Non-static nested class (Inner classes): non static nested classes.
à Member inner class
à Annomynous inner class
à Local inner class.

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...