java protected members (manohar academy)

Опубликовано: 03 Июль 2026
на канале: ManoharAcademy
1,337
10

welcome to manohar academy.
In this lesson we learned about protected members.

Private members can be accessed only in the class they are defined.
Package members can be accessed only in the package in which they are defined.
Protected members can be accessed in the package in which they are defined , and also inside the sub classes in any package.
Public members can be accessed from any where.

Protected access level is more restrictive than public access level, but less restrictive than package access level. It is mainly used for providing direct access to derived classes that exist in any package.

Sub classes can access only their own protected members. They don't have access to super class protected members. This restriction is required to honor encapsulation.

Protected fields are less common and discouraged. But protected methods are beneficial as sub classes can be trusted to know how to call super class protected methods.