Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 924
binarycreations Avatar asked Nov 16 '11 23:11

binarycreations


People also ask

Can abstract class be protected in Java?

Yes, you can declare an abstract method protected. If you do so you can access it from the classes in the same package or from its subclasses. (Any you must to override an abstract method from the subclass and invoke it.)

Why we Cannot declare class as protected in Java?

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

What is protected abstract Java?

Abstract is a programming concept to create a pattern for other classes. protected is an access modifier like public and private. protected variables can be accessed from all classes in your package.

Can you have a protected class in Java?

No, we cannot declare a top-level class as private or protected. It can be either public or default (no modifier).


2 Answers

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.

like image 181
Jon Newmuis Avatar answered Sep 27 '22 16:09

Jon Newmuis


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.

like image 24
Bhesh Gurung Avatar answered Sep 27 '22 18:09

Bhesh Gurung