I have a Class called Module
which has a Method onEnable();
Now i have a class called Config
and want to make the onEnable();
method private
because there is a predefined acting and a class extending Config
should'nt be allowed to change the behaviour.
Is there any way to do this?
Example
class Module{
public void onEnable(){
}
}
A class extending Module which is allowed to use onEnable
:
class HelloWorldModule{
@Override
public void onEnable(){
System.out.println("Hello, World!");
}
}
Now the config Class, where i want that onEnable
is private so that Classes which extend Config cannot! Override onEnable
:
class Config{
@Override
private void onEnable(){
}
}
So NOW, a class named ProgrammConfig
which extends Config
cannot override onEnable
.
However, this is not working because you cannot override a public
method to a private
method.
By declaring a method as public
, you are saying that it should be possible to call said method on every instance of this class, including subclasses. Declaring a method as private
in a subclass doesn't make sense, and is thus not allowed.
Now, if you're concerned about subclasses overriding the method, you can declare the method as final
to prevent this:
class Config extends Module{
@Override
public final void onEnable(){}
//Whatever
}