Thursday, August 30, 2018

Nested Classes in Java

Nested Classes and Private Methods

When you have a class inside another class, they can see each others' private methods. However, this fact is not well known amongst Java developers. Many candidates during interviews will say that private is a visibility that lets code see a member as if it is in the same class. This is actually true, but it would be more precise to say that there is a class that both the code and the member are in. When we have both nested and inner classes, the private member and the code using it can be in the same class, and at the same time, they are also in different classes.

As an example, if I have two nested classes in a top-level class, then the code in one of the nested classes can see a private member of the other nested class.



from DZone.com Feed https://ift.tt/2POw6Ok

No comments:

Post a Comment