Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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

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!

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.