Inner classes

What are advantages of inner class

Questions by jrcheenu   answers by jrcheenu

Editorial / Best Answer

ssalguti  

  • Member Since Apr-2008 | Apr 25th, 2008


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. Nesting such "helper classes" makes their package more streamlined.

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.

Showing Answers 1 - 6 of 6 Answers

ssalguti

  • Apr 25th, 2008
 

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. Nesting such "helper classes" makes their package more streamlined.

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.

goksn

  • Sep 14th, 2009
 

Defining a class within another class is known as nested class.

If class B is defined within class A, then B is known to A, but not outside of A.

Class B has access to the members, including private members of the Class A.

However,the class A does not have access to the members of Class B.

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions