If only one Administrador
can create a Colaborador
, that means that no one else can create it, that is, its builders cannot be public. Remove the public
of the builders of Colaborador
and their subclasses:
public abstract class Colaborador extends Funcionario {
// construtor sem "public"
Colaborador(String nome, String cpf) {
super(nome, cpf);
}
}
public class Entregador extends Colaborador {
// construtor sem "public"
Entregador(String nome, String cpf) {
super(nome, cpf);
}
}
Leaving so, only classes within the same package "see" the constructors. And as the Administrador
is the only one who can create these classes and it is in the same package (com.trabalho1
), that’s exactly what we need.
Therefore, the only point at which a Colaborador
can be created is within classes that are in the same package - and Administrador
is one of them. And if only the Administrador
can create these classes, then you create a public method in this class, which will be responsible for creating collaborators.
Moreover, Colaborador
is an abstract class, and it is not possible to instantiate it. We can only instantiate its subclasses. So one way to do it would be to have a method that received the type of employee to be instantiated, in addition to the information needed to build it (in this case, the name and CPF).
In the example below I am using a int
as the type of collaborator, but in these cases I prefer to use a enum
(like I don’t know if you ever know enum
, I’ll leave the example with int
first):
public class Administrador extends Funcionario {
public Colaborador criaColaborador(int tipo, String nomeColaborador, String cpfColaborador) {
if (tipo == 1) {
return new Entregador(nomeColaborador, cpfColaborador);
} else if (tipo == 2) {
// retorna outro tipo de colaborador (Secretaria, Gerente, etc)
}
// vários if's, para cada tipo de colaborador
// tipo desconhecido, retorna null (ou algum valor default)? lança exception? você decide
return null;
}
}
In case you already know enum
, gets like this:
public enum TipoColaborador {
SECRETARIA, GERENTE_VENDA, ENTREGADOR;
}
public class Administrador extends Funcionario {
public Colaborador criaColaborador(TipoColaborador tipo, String nomeColaborador, String cpfColaborador) {
if (tipo == TipoColaborador.ENTREGADOR) {
return new Entregador(nomeColaborador, cpfColaborador);
} else if (tipo == TipoColaborador.GERENTE_VENDA) {
// retorna outro tipo de colaborador (Secretaria, Gerente, etc)
}
// vários if's, para cada tipo de colaborador
// retorna null (ou algum valor default)? lança exception? você decide
return null;
}
}
I’d rather use enum
because it makes it clear which values can be passed on the type. If I use int
, I can unintentionally pass some number that has nothing to do with the guy, for example.
Remembering that this is just one way of doing. It is also possible to have a method for each type (criarEntregador
, criarSecretaria
, etc), use Map
to map types and their classes (Map<TipoColaborador, Class<? extends Colaborador>>
), among others (in addition to the obvious "use switch
instead of if
", if you already know).
But the general idea for your problem is this. By not letting the class have public constructors, you limit and control who can create instances of it. In this case, the only place in which these builders are called is in the public method that is in Administrador
, which means that only this class can create instances of Colaborador
.
To create your instances, would look like this:
Administrador admin = new Administrador("Admin", "12345678909");
Colaborador col = admin.criaColaborador(TipoColaborador.ENTREGADOR, "Fulano de Tal", "11122233396");
Seria that what you seek?
– hkotsubo
I couldn’t do it with this method.
– Wesley Belizario