First time here? Check out the FAQ!

"Private Protected" access specifier in Java.

0 votes
asked by
edited by

How can I use the access specifier "private protected" in Java?

1 Answer

0 votes
answered by (80k points)
edited by

An object that is declared private can be accessed only inside the declared class itself. This is the most restrictive access level but you can use variables that are declared private only if in the same class they are some methods like get() or set() that are declared public.

Objects that are declared protected in a super class can be accessed only by the package of the protected member class or by any subclass of it. This gives subclasses the access to use helper method or variables but stops the non-related classes from trying to access it's members.

One of the main concepts of OOP is encapsulation. This means that you have to make your classes as private as possible and to be careful about what members you should make private or protected.

Also, as for version 1.0.2 of JDK, you can not use private and protected together.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register