0
I don’t understand why the variable descricao
not producing the values. In this case, it should go out like this:
Old charger connecting two-pin connector socket
But it’s coming off:
Old charger connecting NULL socket
Follows the class TomadaDeDoisPinos
:
package Adapter;
public class TomadaDeDoisPinos implements TomadaDeDoisPinosIF{
@Override
public void conectar(ConectorDeDoisPinos conector) {
System.out.println(conector.getDescricao());
}
}
Follows the class ConectorDeDoisPinos
:
package Adapter;
public class ConectorDeDoisPinos {
protected String descricao;
public String getDescricao(){
this.descricao =" conector de dois pinos.";
return this.descricao;
}
}
Follows the class CarregadorAntigo
:
package Adapter;
public class CarregadorAntigo extends ConectorDeDoisPinos{
public String getDescricao(){
return "Carregador antigo conectando a tomada de "+descricao;
}
}
Follows the interface TomadaDeDoisPinosIF
:
package Adapter;
public interface TomadaDeDoisPinosIF {
public void conectar(ConectorDeDoisPinos conector);
}
Follows the class of Teste
:
package Adapter;
public class Teste {
public static void main(String[] args) {
ConectorDeDoisPinos cAntigo = new CarregadorAntigo();
TomadaDeDoisPinos tomadaDeDoisPinos = new TomadaDeDoisPinos();
tomadaDeDoisPinos.conectar(cAntigo);
}
}
With the current code it is not possible to reproduce the problem. I made an adaptation to compile and presented another result. Give us more information so we can help you. http://ideone.com/mTi9PP
– Maniero
I put the old Loader class, which was missing
– Jose.Lemo
You need to initialize the member, how you want to do this?
– Maniero
I want the variable of the class Connector of two pins to be initialized in the method getDescription within the class that is, but it is null as it is,
– Jose.Lemo
Take care of implementation inheritances, especially when you overwrite inherited code: This is a good way to turn your code into a bug-infested spaghetti and clogged with gambiarras. Your code implies that you don’t quite understand how method overwriting works and even when you should or should not apply it, so I recommend studying this topic well. Ah, and by the way, altering fields of objects inside getters is opening a pandora’s box, will only give you trouble and headaches.
– Victor Stafusa