I am reading some Java text and the text says that we can only apply public
or default
access modifier for class and interface. Therefore, it is a compiling error if we declare:
private class A {}
or
protected class A{}
I am just curious why a class or an interface cannot receive private
or protected
access modifiers?
Since 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.
Interface Access Modifiers Java interfaces are meant to specify fields and methods that are publicly available in classes that implement the interfaces. Therefore you cannot use the private and protected access modifiers in interfaces.
Protected methods are intended for sharing implementation with subclasses. Interfaces have nothing to offer as far as implementation sharing goes, because they have no implementation at all. Therefore all methods on interfaces must be public.
Interface members are always public because the purpose of an interface is to enable other types to access a class or struct. No access modifiers can be applied to interface members.
But you should remember that in earlier versions of C# you can’t have any explicit access modifier to any member of the interface. All are public by default. It is a compile-time error if you try to add any access modifiers, even public to any interface member. Can you have private interface members in C#?
Private access modifier. The scope of private modifier is limited to the class only. Private Data members and methods are only accessible within the class. Class and Interface cannot be declared as private. If a class has private constructor then you cannot create the object of that class from outside of the class.
Protected access modifier example in Java. In this example the class Test which is present in another package is able to call the addTwoNumbers() method, which is declared protected. This is because the Test class extends class Addition and the protected modifier allows the access of protected members in subclasses (in any packages).
If you try to declare the members of an interface private, a compile-time error is generated saying “modifier private not allowed here”. In the following Java example, we are trying to declare the field and method of an interface private. On compiling, the above program generates the following error.
private
means "only visible within the enclosing class".
protected
means "only visible within the enclosing class and any subclasses, and also anywhere in the enclosing class's package".
private
, therefore, has no meaning when applied to a top-level class; the same goes for the first part of the definition of protected
. The second part of protected
could apply, but it is covered by the default (package-protected) modifier, so protected
is part meaningless and part redundant.
Both private
and protected
can be (and frequently are) applied to nested classes and interfaces, just never top-level classes and interfaces.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With