Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Usage of inner class

Tags:

I can understand what inner class is and how to write program. My question is in what situation do programmers really need inner class?

like image 538
Sumithra Avatar asked Oct 29 '10 07:10

Sumithra


1 Answers

Sometimes there is some functionality which is best represented as an object, but which is only meaningful within the context of another object, which does not necessarily need to be exposed to the outside world, and which can benefit from having access to the parent classes data (so as to not violate encapsulation).

The best example that I can think of is putting a Node class inside of a LinkedList. Nodes are only meaningful to the LinkedList, so they only exist within one. No one outside of the LinkedList cares about nodes or should have access to them.

like image 184
Reese Moore Avatar answered Oct 22 '22 05:10

Reese Moore