Home:ALL Converter>Why can a class not be defined as protected?

Why can a class not be defined as protected?

Ask Time:2010-10-06T12:46:13         Author:M.J.

Json Formatter

Why can we not define a class as protected?

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

Author:M.J.,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/3869556/why-can-a-class-not-be-defined-as-protected
k-s :

The answer from @Akash5288 made no sense to me:\n\nIf all the classes are allowed to subclass then it will be similar to public access specifier.\nSince there is no way to restrict this class being subclassed by only few classes (we cannot restrict class being inherited by only few classes out of all the available classes in a package/outside of a package), there is no use of protected access specifiers for top level classes. Hence it is not allowed.\n\nYou can then apply the same logic to protected methods and variables, they are also then "similar to public". All classes outside of a package can extend our public class and use its protected methods. Why is restricting methods and variables to extended classes ok, but restricting the whole class not ok? "Similar to public" is not "same as public". My interpretation is that it is perfectly fine to allow a protected class, as it is fine to allow protected methods.\nThe answer "you can not extend a class you can not access/see" is more logical.",
2018-04-12T02:28:30
Nikita Rybak :

Because it makes no sense.\n\nProtected class member (method or variable) is just like package-private (default visibility), except that it also can be accessed from subclasses.\nSince there's no such concept as 'subpackage' or 'package-inheritance' in Java, declaring class protected or package-private would be the same thing.\n\nYou can declare nested and inner classes as protected or private, though.",
2010-10-06T04:49:13
Anurag Prasad :

What makes sense to this question is that, JVM is written in C (Sun JVM) and C++(oracle JVM) so during compilation, we are going to create .class files out of our java file and if we declare a class with Protected keyword then it will not be accessed by JVM. \n\nThe answer why protected class will not be accessed by JVM is that, since protected fields are accessible within same package or to diffrent package through inheritance only and JVM is not written in a way so that it will inherit will class. \nHope this satisfies this question :) \n\nSimilarly, A top level class can't be private. Explanation as below:\n\nSo what will happen if we will define a class private, that class will only be accessible within the entity in which it is defined which in our case is its package?\n\nSo defining private access to the class will make it accessible inside the same package which default keyword already do for us, Therefore there is no benefit of defining a class private it will only make things ambiguous.",
2018-06-14T12:55:20
Akash5288 :

As you know default is for package level access and protected is for package level plus non-package classes but which extends this class (Point to be noted here is you can extend the class only if it is visible!). \nLet's put it in this way: \n\n\nprotected top-level class would be visible to classes in its package. \nnow making it visible outside the package (subclasses) is bit confusing and tricky. Which classes should be allowed to inherit our protected class? \nIf all the classes are allowed to subclass then it will be similar to public access specifier. \nIf none then it is similar to default. \n\n\nSince there is no way to restrict this class being subclassed by only few classes (we cannot restrict class being inherited by only few classes out of all the available classes in a package/outside of a package), there is no use of protected access specifiers for top level classes. Hence it is not allowed.",
2013-04-30T05:50:19
Yogesh Patil :

protected means that the member can be accessed by any class in the same package \nand by sub classes even if they are in another packages.\n\nExample:\n\npackage a;\nclass parent{\n protected void p();\n}\npackage b;\nimport a.p;\nclass child extends parent{\n //you can access method which is protected in the parent in the child \n}\nclass another extends child {\n //here you can not access the protected method \n}\n",
2018-09-01T19:22:12
irreputable :

public class A\n{\n protected class B\n {\n }\n}\n",
2010-10-06T04:51:13
Naresh Joshi :

Defining a field protected makes that field accessible inside the package as well as outside the package through inheritance only (Only inside the child class).\n\nSo If we are allowed to make a class protected then we can access it inside the package very easily but for accessing that class outside of the package we first need to extend that entity in which this class is defined which is its package.\n\nAnd since a package can not be extended (can be imported), defining a class protected will again make it package-private which is similar to defining it as default which we can already do.\nTherefore there is no benefit of defining a class private it will only make things ambiguous.\n\nFor more information read Why an outer Java class can’t be private or protected",
2016-10-12T10:31:47
林果皞 :

@Nikita Rybak answer has good points but lack of details, i can't simply get the idea without think deeply myself, the following is what i thought and now i should completely understood the reason.\n\nFour access modifiers, assume the 1st level is public and 4th level is private (based on this table in sequence). The first thing we should know is why class cannot defined as private in top-level.\n\nSo if \"private class foo\"(A private member defined, i.e. class itself is a member) allow, what is the outer (which contains the member) ? File scope ? No, file outer is pointless because even multiple classes in single file will be compile into separate class files. So the outer is package. But the 3rd level default access modifier already means \"package-private\". So the 4th level private access modifier will not be used/allowed. \n\nBut nested private class is allow because the direct outer is class, not package, e.g.:\n\nclass PrivateNestedMain {\n private static class Inner {\n public static void main(String[] args) {\n System.out.println(\"Hello from Inner!\");\n }\n }\n}\n\n\nNow what if \"protected class foo\" allow ? protected main characteristic is subclass, so the outer(package) SHOULD(due to up-to scope, but still it's optional) provide style of subclass, i.e. sub-package, or package A extends package B, but we know no such thing. So protected can't use full potential(main scope is subclass-wide) in top-level which the outer is package(i.e. no such sub-package thing), but protected can use full potential in nested class which the outer is class(i.e. can be subclass):\n\nclass ProtectedNestedMain {\n protected static class Inner {\n public static void main(String[] args) {\n System.out.println(\"Hello from Inner!\");\n }\n }\n}\n\n\nNote that the above said \"can't use full potential\" due to it can't reach subclass-wide merely because no outer subclass, that's means actually protected can be allow, it's just a matter of choice to avoid duplicate the job of package-private if outer not subclass-able, see below.\n\nMy confusing is mainly caused by the famous table at https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html:\n\n\n\nIf 1st level(public) and 3rd level (package-private) allowed, how on earth the in-between 2nd level (protected) not allowed ?\n\npublic support subclass so easy to misleading. The correct way to read this table is \n\n\n public support subclass if the outer has subclass feature.\n\n\nThe same misleading apply to package-private, package-private doesn't support subclass (N in cell) doesn't means subclass concept apply in outer.\n\nThat's means we should ignore the Subclass column if subclass feature is not available in outer:\n\n\n\nAs we can see now, both protected and package-private are the same level now (Y-Y-N), no more confusion about why in-between level is not allowed. Overall, Java pick only package-private over protected to avoid confusing(it's just a matter of choice, but protected main characteristic is subclass, so package-private is superior), and the result, only 2 access modifiers allowed in top-level:\n\n\n At the top level—public, or package-private (no explicit modifier).\n",
2016-05-31T20:37:37
Shruthi reddy :

Protected is not similar to public. Protected has both package level access plus can be accessed outside of packages only by inheritance..If a class say A outside a package INHERITS a class from other package(with protected method by using INHERITANCE) it can access the methods of this class B which has protected methods but the sub-classes derived from this class i.e., A can't access the protected methods..the opposite happens with public..\n\nExample:\n\npackage 2;\nclass B\n{\nprotected void method1()\n{\n}\n}\npackage 1;\nimport 2.B;\nclass A extends B\n{\n//can access protected method\n}\nclass C extends A\n{\n//can't access the protected method\n}\n",
2015-03-27T17:03:04
Jeen Yung :

if a outer class is declared by protected, I think you want the class can be only accessed from same package and its subclass but different packages. However, there is no possible to create subclasses for a protected class, because when you write "class Dog extends Animal", because of protected "Animal" only can be accessed by its subclass, obviously, "Dog" is not "Animal" subclass.\nSo protected outer class is same with (default) outer class!",
2020-09-28T10:02:03
madhu :

behavior of “protected” = behavior of “default”+ “use it in any subclass in any package”.\n\nAnyway we have default access modifier for class, only advantage we can get from protected access modifier is:- by using it in any package through subclassing. But for subclass, visibility of parent “protected”class would be private. So it can’t be accessed. Basically if you have a protected top-level class, no outer class can gain access by subclassing it. So protected for a top-level class is meaningless.",
2014-07-03T15:43:14
Narendra Singh :

Protected : VISIBLE only to package level*.\n\nclass is defined protected ---> it cannot be extended from outside package(not visible).\n\nAnd if it cannot be extended then it is meaningless to keep it as protected, because then it will become default access which is allowed.\n\nSame applies to private defined classes.\n\nNote : Nested or inner classes can be defined protected or private.\n\n* : Explore protected keyword, for this answer I made it succinct.",
2018-03-14T08:42:18
yy