Why don't you have a secure abstract class in Java?

I have an abstract class that looks like this:

abstract class AbstractFoo implements Bar { //Code goes here } 

However, when I try to protect AbstractFoo, I get a compile-time error of the error, complaining that it is an illegal modifier.

 protected abstract class AbstractFoo implements Bar { //Code goes here } 

Why don't you have a secure abstract class in Java?

EDIT: I should probably mention that this is not vanilla Java and is actually Blackberry / J2ME.

+6
source share
6 answers

As many others have noted, the limitation here has nothing to do with the fact that your class is abstract, but rather the visibility modifier that you have chosen to use. Keep in mind that each of these visibility modifiers means:

For the class in which you use the keyword ...

  • private: Only for this class

  • (default / package closed): Only visible to this class and classes in the package

  • protected: Visible for this class, classes in its package and subclasses of this class

  • public: Visible to any class

Top-level classes cannot be declared private , because then nothing can access them.

But your question is related to why they cannot be declared protected . It is clear that a protected top-level class (if it could exist) would be visible to itself (each class is visible on its own). It is also clear that the protected top-level class will be visible to classes in its package. However, making it visible to your subclasses is difficult. Which classes should be allowed to inherit our protected class?

If that’s all, then our class can also be public , because then we say that any class has the right to access our protected class. If this is not one of them, then our class can also be package-private , since only two other conditions are fulfilled (visible to oneself and things in its package). And this cannot be β€œsome of them,” because we need a way to determine which classes can access it, which was primarily a visibility modifier.

Thus, for these reasons, top-level classes cannot be private or protected ; they must be with the package β€” private or public .

+19
source

Top-level classes can only be publicly available or package-private (default).

 public class PublicClass { protected class InnerClass { } //protected makes sense here } class PackagePrivateClass { } 

Since: PublicClass and PackagePrivateClass are both top-level classes, here they cannot have other access modifiers, private and protected .

For top-level classes, only public and standard access modifiers are allowed . But other modifiers are also allowed for inner member classes.

This essay has nothing to do here.

+6
source

You can; The following code compiles fine :

 public class Main { interface Bar {} protected abstract class AbstractFoo implements Bar {} public static void main(String[] args) {} } 
+4
source

I do not believe in the premise of the question. I have successfully compiled the code below:

 class prot { public abstract class pubab { } protected abstract class protab { } abstract class packprivab { } private abstract class privab { } } 

which tells me that you can have a protected abstract class in java. In this case, you can have a private abstract class.

Have you tried to have a top-level protected class (not allowed)?

0
source

IMO, it looks like an illegal modifier, because protected means that it must be visible in package + in derived classes. But in order to declare that some class extends this protected class, you first need to see it from there, which cannot be, because it is visible only from derived classes (given that superclasses and subclasses are not in the same volume same package).

If you want to see only a class from other classes in the same package, then by default (package-private) the modifier will work, there is no reason to protect the protected modifier - it adds an absolutely illogical and useless ability to see a class from its derived classes, which

a) cannot happen if this class and its derived classes are not in the same package.

b) it can happen, given that this class and its derived classes are in the same package - this will work with the default modifier (package-private) in any case.

0
source

Top-level classes cannot have a protected scope, only public or package . Here's a good link explaining the permissible use of scoping modifiers for classes.

0
source

Source: https://habr.com/ru/post/901671/


All Articles