Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't a class or an interface receive private or protected access modifiers?

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?

like image 947
ipkiss Avatar asked Jul 18 '11 14:07

ipkiss


People also ask

Why we Cannot declare class interface with private or protected access specifiers?

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.

Can we use private and protected access modifiers inside an interface?

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.

Why interface Cannot have protected methods?

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.

Why interface methods Cannot have access modifiers?

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.

Can you have a public access modifier to an interface member?

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#?

What is the scope of private access modifier?

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.

What is protected access modifier in Java?

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).

What does “modifier private not allowed here” mean?

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.


1 Answers

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.

like image 195
Michael Myers Avatar answered Oct 12 '22 16:10

Michael Myers