Why can a class not be defined as protected?

后端 未结 12 2143
长发绾君心
长发绾君心 2020-12-04 08:03

Why can we not define a class as protected?

I know that we can\'t, but why? There should be some specific reason.

12条回答
  •  被撕碎了的回忆
    2020-12-04 08:43

    protected means that the member can be accessed by any class in the same package and by sub classes even if they are in another packages.

    Example:

    package a;
    class parent{
     protected void p();
    }
    package b;
    import a.p;
    class child extends parent{
      //you can access method which is protected in the parent in the child 
    }
    class another extends child {
     //here you can not access the protected method 
    }
    

提交回复
热议问题