@Ana, in addition to using the constructor, validates the methods, so you create invariants for your class and impose rules for the states your class may be in. In other words, it is not enough to create a manager with a number of quantityFunctures
It is important to ensure that this quantidaed is >= 0
(greater than or equal to zero) or even greater than zero.
Another important validation at the time of creation, is to make sure that users of your class will not define a name nulo
or empty ""
public Gerente(String nome, int quantidadeFuncionarios) {
if(nome == null || nome.isEmpty()){
throw new IllegaArgumentException("Nome não pode ser vazio");
}
if(quantidadeFuncionarios < 0){
throw new IllegalArgumentException("Quantidade funcionarios não opde ser inferior a zero");
}
this.setNome(nome); // herdando do funcionário
this.quantidadeFuncionarios = quantidadeFuncionarios;
}
The best way to really force your objects to be in a valid state is this.
Ch the documentation this exception: http://docs.oracle.com/javase/7/docs/api/java/lang/IllegalArgumentException.html
Consider making your objects immutable.
Another way to ensure the invariants of your class is to redesign it to be immutable.
There are some trade offs to be considered, check this tutorial:
http://www.caelum.com.br/apostila-java-testes-xml-design-patterns/o-modelo-da-bolsa-de-valores-datas-e-objetos-imutaveis/
I don’t quite understand your question, but if Manager is Employee’s son,
nome
will also be a private Manager attribute.– Felipe Avelar