Why is the description variable coming out null?

0

I do not understand why the variable descricao is not producing the values. In this case, it should look like this:

Carregador antigo conectando a tomada de conector de dois pinos

But it's coming out:

Carregador antigo conectando a tomada de NULL

The class TomadaDeDoisPinos follows:

package Adapter;

public class TomadaDeDoisPinos implements TomadaDeDoisPinosIF{

    @Override 
    public void conectar(ConectorDeDoisPinos conector) {
        System.out.println(conector.getDescricao());  
    } 
}

The class ConectorDeDoisPinos follows:

package Adapter;

public class ConectorDeDoisPinos {

    protected String descricao;    

    public String getDescricao(){  
        this.descricao =" conector de dois pinos."; 
        return this.descricao;   
    } 

}

The class CarregadorAntigo follows:

package Adapter;

public class CarregadorAntigo extends ConectorDeDoisPinos{


    public String getDescricao(){  
        return "Carregador antigo conectando a tomada de "+descricao;   
    }    
} 

Follow the TomadaDeDoisPinosIF interface:

package Adapter;

public interface TomadaDeDoisPinosIF { 

    public void conectar(ConectorDeDoisPinos conector);

} 

The class of Teste follows:

package Adapter;

public class Teste {

    public static void main(String[] args) {
        ConectorDeDoisPinos cAntigo = new CarregadorAntigo();
        TomadaDeDoisPinos tomadaDeDoisPinos = new TomadaDeDoisPinos();

        tomadaDeDoisPinos.conectar(cAntigo);       

    }   
}
    
asked by anonymous 09.12.2015 / 23:46

1 answer

2

This code seems to be quite wrong. But I do not know, it could be an initial exercise. Either way you will be learning in a way that I think is wrong.

To fix the problem presented, it would be enough to initialize the class member so that it does not get null. The fact of inheriting the structure of one class in another does not mean that it will inherit everything that happens to it. So just change the method of the class that creates the object like this:

class CarregadorAntigo extends ConectorDeDoisPinos{

    @Override 
    public String getDescricao(){  
        this.descricao =" conector de dois pinos."; 
        return "Carregador antigo conectando a tomada de " + descricao;   
    }    
}

See running on ideone .

    
10.12.2015 / 00:58