Why can't you have a protected abstract class in Java?

时光毁灭记忆、已成空白 提交于 2019-12-04 18:50:39

问题


I have an abstract class which looks like:

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

However when I try to make AbstractFoo protected I get an error compile time error complaining that it is an illegal modifier.

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

Why can't you have a protected abstract class within Java?

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


回答1:


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

For the class in which you are using the keyword...

  • private: Only visible to this class

  • (default/package private): Only visible to this class and classes in its package

  • protected: Visible to 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 would ever be able to access them.

But your question stems around why they may not be declared protected. It is clear that a protected top-level class (were it able to exist) would be visible to itself (every class is visible to itself). It is also clear that a protected top-level class would be visible to classes in its package. However, making it visible to its subclasses is tricky. Which classes should be allowed to inherit our protected class?

If it is all of them, then our class might as well be public, because then we're saying that any class has the right to access our protected class. If it's none of them, then our class might as well be package-private, since only the other two conditions (being visible to itself and things in its package) are met. And it can't be "some of them," since we would need a way of defining which classes can access it, which is what the visibility modifier was for in the first place.

So for these reasons, top-level classes cannot be private or protected; they must be package-private or public.




回答2:


Top level classes can only be public 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.

Only the public and the default access-modifiers are allowed for the top-level classes. But for the inner member classes other modifiers are also allowed.

That abstract has nothing do here most probably.




回答3:


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:


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

class prot
{
    public abstract class pubab
    {
    }

    protected abstract class protab
    {
    }

    abstract class packprivab
    {
    }

    private abstract class privab
    {
    }
}

which suggests to me you can have a protected abstract class in java. For that matter you can have a private abstract class.

Did you try to have a protected top-level class (not allowed)?




回答5:


IMO, it does seem like an illegal modifier because protected means it should be visible in the package + in the derived classes. But in order to declare that some class extends this protected class you first need to be able to see it from there, which can't happen because it's visible only from the derived classes(given that super class and subclasses aren't in the same package).

If you only want to be able to see the class from other classes in the same package the default(package-private) modifier will work, no reason for the protected modifier to work - it only adds a completely unlogical and useless ability to see the class from its derived classes, which

a) can't happen if this class and its derived classes aren't in the same package.

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




回答6:


Top level classes cannot have protected scope, only public or package. Here's a nice reference explaining the allowable uses of scoping modifiers on classes.



来源:https://stackoverflow.com/questions/8160019/why-cant-you-have-a-protected-abstract-class-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!