java – Create a "hidden" class only accessible from another

Question:

I am creating an API and I would like the user to only depend on one class, so that they do not have to search through several and it is easier (For this example, I will use the names ClassA and ClassB, ClassA being the public one and ClassB the one that I want to "hide").

I know the basic idea:

public class ClaseA extends ClaseB{

    public ClaseA(){

    }
}

In this way, I can call all the functions of ClassB also from ClassA.

My question is: How can I make this class "invisible" for the API user? I mean, how do I make the user unable to use ClassB? I need that, if you want to use a function of ClassB, you do it by calling from ClassA.

Thank you very much to all.

Answer:

You can set the visibility of the parent class to just the package, removing all the visibility modifiers (class A). Child classes should be in the same package as public (public class B).

class A {
    ...
}

public class B extends A {
    ...
}
Scroll to Top