Alex Rivera | Logout

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

Asked 2011-11-16T23:06:18.903
16

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.

Edit
Report

2 Answers

6

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.

answered 2011-11-16T23:08:16.897
0

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.

answered 2011-11-17T00:15:12.020

Your Answer