A Java method that can only be called by its own class or other subclasses

I want my class to have a method that can only be called by itself or its subclasses

The closest is protected access, but it also allows other classes in the same package to call this method, which I don't want

Is there a way to do this, or should I consider some kind of package refactoring?

In my opinion, private won't let subclasses use this method

Solution

See in Java, difference between default, public, protected, and private

There's no such thing But you can move the class in an empty package and use protected

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>