Create a "hidden" class only accessible from another

5

I am creating an API and I would like the user to only depend on one class, so that he does not have to search through several and be simpler (For this example, I will use the names ClassA and ClassB, ClassA being the public and Class B I want to "hide").

The basic idea I know:

public class ClaseA extends ClaseB{

    public ClaseA(){

    }
}

In this way, I can call all Class B functions also from ClassA.

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

Thank you very much everyone.

    
asked by Mayuso 11.10.2016 в 08:43
source

4 answers

1

I think what you want is to define an interface for your API, in the interface you put the functionality, it's like a contract between who uses the interface and implements it:

interface miAPI
{
    int hazAlgo();
    int hazOtraCosa(String dato);
}

So the public part of your api only returns the interface, regardless of the object that implements it

public static class API
{
    static miAPI getApi()
    {
        //aquí puedes meter la lógica que necesites o usar el patrón de creación que estimes
        return new miImplementacion();
    }
}

Use from outside would be for example:

int usarAPI()
    {
        API api = API.getApi();
        return api.hazAlgo() + api.hazOtraCosa("hola");
    }

You do not have access to the class that implements your API or your API part

What is missing is the api provider:

public static class API
{
    static miAPI getApi()
    {
        //aquí puedes meter la lógica que necesites o usar el patrón de creación que estimes
        return new miImplementacion();
    }
}

and the private class that implements your API:

private static class miImplementacion implements   miAPI
{

    @Override
    public int hazAlgo()
    {
        return 0;
    }

    @Override
    public int hazOtraCosa(String dato)
    {
        return 0;
    }

}

This is a very simple illustrative example. I recommend that the interfaces keep them related to their responsibility and that they do only the part they know, for example if the interface is Alumno , that it has only asistirAClase and hacerExamen , if you need corregirExamen should go in other interface Profesor I usually name all interfaces with an i in front, so I always know that they are interfaces: iAlumno, iProfesor.

I recommend that you read about design patterns: Design patterns

note: a class can implement several interfaces, following the example of student / teacher with another way to offer your api:

public interface iAlumno
{
    String hacerExamen(String examen);
    String asistirAClase(String clase);
}
public interface iProfesor
{
    String crearExamen();
    int corregirExamen();
}


public class ClaseAPI implements iAlumno, iProfesor
{
    private iAlumno alumno;
    private iProfesor profesor;
    ClaseAPI()
    {
        alumno = new Alumno();
        profesor = new Profesor();
    }

    @Override
    public String hacerExamen(String examen)
    {
        return alumno.hacerExamen(examen);
    }

    @Override
    public String asistirAClase(String clase)
    {
        return alumno.asistirAClase(clase);
    }

    @Override
    public String crearExamen()
    {
        return profesor.crearExamen();
    }

    @Override
    public int corregirExamen()
    {
        return profesor.corregirExamen();
    }
}
private class Alumno implements  iAlumno
{

    @Override
    public String hacerExamen(String examen)
    {
        return null;
    }

    @Override
    public String asistirAClase(String clase)
    {
        return null;
    }
}

private class Profesor implements iProfesor
{

    @Override
    public String crearExamen()
    {
        return null;
    }

    @Override
    public int corregirExamen()
    {
        return 0;
    }
}
    
answered by 11.10.2016 / 19:27
source
2

You can put the visibility of the parent class to only the package, removing all the visibility modifiers (class A). The child classes should be in the same package as public (public class B).

class A {
    ...
}

public class B extends A {
    ...
}
    
answered by 11.10.2016 в 11:07
2
class B {
    protected B(){
    //constructor
    }
    protected void method(){
       //Cuerpo
    }
}

in the same package ..

  public class A extends B{
     public void methodPublico(){
        super.method();
    }
  }
    
answered by 11.10.2016 в 17:37
1

Hide class no This would be an example, you define a class you have a ClaseA that inherits from a ClaseB , the ClaseB is defined as abstract so that it can not be instantiated (you can not create an object of the type ClaseB ).

Within ClaseB we define a private method, which can only be accessed by ClaseB .

As an example, an instance of ClaseA is created but the method that can be accessible is only that of ClaseA and not the ClaseB of which we inherited:

abstract class ClaseB{        

    public ClaseB() {    
        System.out.println("ClassB");
    }

   private void realizaTarea() {
        System.out.println("realizaTarea() de Clase B");
    }
}

class ClaseA extends ClaseB {    
       public ClaseA() {
          System.out.println("ClassA");
    }    
     public void realizaTarea() {
        System.out.println("realizaTarea() de Clase A");
    }
}

public class JavaApplication1 {        
    public static void main(String[] args) {
     // Se instancia ClaseA que hereda de ClaseB.
     ClaseA a = new ClaseA();
       //El método que puede ser accesible es el de ClaseA y no de ClaseB.
       a.realizaTarea();
     }             

}

The previous example will have as output:

ClassB
ClassA
realizaTarea() de Clase A
    
answered by 11.10.2016 в 17:02