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(); }
If a method of a class is private, you cannot access it outside the current class, not even from the child classes of it. But, incase of an abstract method, you cannot use it from the same class, you need to override it from subclass and use. Therefore, the abstract method cannot be private.
To implement features of an abstract class, we inherit subclasses from it and create objects of the subclass. A subclass must override all abstract methods of an abstract class. However, if the subclass is declared abstract, it's not mandatory to override abstract methods.
Rules For using Private Methods in InterfacesPrivate interface method cannot be abstract and no private and abstract modifiers together.
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 :
abstract
MethodsIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With