MY mENU


Thursday 9 February 2012

Advantages of InnerClass


There are several reasons for using Nested Classes(InnerClass) :

- It is a way of logically grouping classes that are only used in one place.
- It increases encapsulation.
- Nested classes can lead to more readable and maintainable code.

Logical grouping of classes : 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.

Increased 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.

More readable, Maintainable code : Nesting small classes within top-level classes places the code closer to where it is used.

No comments:

Post a Comment