Java Java Data Structures Efficiency! Custom Serialization

SungGeun Kim
SungGeun Kim
9,072 Points

Just make sure the concept of protected here. If we want it to be package-protected. We can omit the access modifier?

From java doc, I found https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html where it shows protected also allow subclass access whereby the no-access modifier could be used for package-protection. Now in your tutorial, you wanted package protection, so I guess the no-access modifier is what you wanted?

2 Answers

If I read this right,

By default if you don't put private/public only the class can access that variable. It is similar if you put private. But there is a difference between the two and reading the docs will tell you the difference.

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

Nice attention to detail!

I haven't delved to deeply into subclasses yet, but creating a subclass in another package would allow access. I wanted to show off the protected keyword for times when you might run across it. I think in this example, it would be okay for a subclass to do the member variable access like we did, but you're right, it's not just package protected, it's children would have access.

That make sense? Thanks for pointing that out!