Andrew No Andrew No - 5 months ago 8x
Java Question

Java any way to keep a method usable only by class and subclasses?

I am fully aware of how

in Java work. I was wondering if there is a way to mimic a modifier in between private and protected. I want only a class and its children to be able to access its children.

I want to do this because I have a class,
that stores an
int databaseID
has a
method that stores the DBID then calls
protected abstract ContentValues getSaveData ()
to obtain any other information from its children to write into the DB. This however, is unsafe design.
will store all of the objects information, private or not to be written into the DB.

Now, while I could just make save an abstract method in
, I would prefer to keep the structure I have now, as I will be adding data to the
class later on, and I would prefer to never have to expose this data to the user.

So, is there any way to mimic a children-only modifier in Java? If not, I may store the encrypted value of all the data the user supplies in

Requested code: Removed as it is no longer necessary.


So, is there any way to mimic a children-only modifier in Java?

Nope. The only access modifiers we've got are the ones you listed. There's also "default" visibility, aka "package-private", but that does not make elements accessible to subclasses. So you could probably just use protected and not worry about it.

It sounds like a better solution might be a refactor of your code, but there's not much concrete in your question to go on.