Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
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.