Reputation: 2053
Is it possible to define a private abstract class in Java? How would a Java developer write a construct like below?
public abstract class MyCommand {
public void execute()
{
if (areRequirementsFulfilled())
{
executeInternal();
}
}
private abstract void executeInternal();
private abstract boolean areRequirementsFulfilled();
}
Upvotes: 36
Views: 44893
Reputation: 41617
That would be protected
instead of private
. It means that only classes that extend MyCommand
have access to the two methods. (So do all classes from the same package, but that's a minor point.)
Upvotes: 8
Reputation: 93157
You can't have private abstract
methods in Java.
When a method is private
, the sub classes can't access it, hence they can't override it.
If you want a similar behavior you'll need protected abstract
method.
It is a compile-time error if a method declaration that contains the keyword
abstract
also contains any one of the keywordsprivate
,static
,final
,native
,strictfp
, orsynchronized
.
And
It would be impossible for a subclass to implement a
private abstract
method, becauseprivate
methods are not inherited by subclasses; therefore such a method could never be used.
Resources :
Upvotes: 73