lundi 20 avril 2015

Is there any way to define a private class in Java which only other classes in the same package can access it?

I've always had this question on top of my mind that why defining private classes within a package in Java is not possible like the way we can define it in a namespace in .Net.

Let's say I have a ds package in my Java Project. I have two classes in the names of Stack and Queue inside this package. Both of these classes need to make use of another class in the name of ListNode. ListNode is only required by other classes in the ds package, and it shouldn't be exposed to classes outside of the ds package.

I can define the ListNode as an inner classes. But in this case, I should define it in both Stack and Queue classes, and it's also redundant. Is there any design pattern to work around this? If not, please someone at least explain me why this is not possible in Java!

Aucun commentaire:

Enregistrer un commentaire