Why Java class Cannot be Protected? | Core Java Training

This Core Java Training “Java Class cannot be protected?” post explains clearly why we cannot define class as protected in java and where to access protected members of class in Java application.


 

protected class in Java :

  • We can apply ‘protected’ modifier to variables, methods and constructor.
  • We cannot apply ‘protected’ modifier to class.
  • Protected members can be accessed with in the package and only from sub classes of Child class.
  • We can access protected members of class only with in the hierarchy(family) in inheritance.

Access Protected members in Java

 

  • We cannot define class as protected.
  • If complete class is protected then the class is not visible to outer package classes to communicate.
  • Such type of objects do not exists in real world. Hence class cannot be protected.

Java class cannot be Protected

 

Program that gives error if we define class as protected:

 

Share this