Home > Enterprise >  How to use toString method with private attributes in different classes?
How to use toString method with private attributes in different classes?

Time:12-10

Let the vehicle class a mother class and car class a extend of the vehicle class :

The mother class :


public abstract class vehicle {
    private String imatriculation ; 
    private  int  nb_chevaux ; 
    private  double consomation ; 
    
    public vehicle(String imatriculation , int nb_chevaux , double consomation){ 

        this.imatriculation = imatriculation ;
        this.nb_chevaux = nb_chevaux ;
        this.consomation = consomation ;
    }
    
    public abstract void setconsomation(int conso_input );     
}

Car inherit from vehicle :

public class car extends vehicle {
    
    public car(String imatriculation,int nb_chevaux,double consomation){
    super(imatriculation ,nb_chevaux , consomation ) ; 
    }     
    @Override 
    public void setconsomation(int conso_input ){
        this.consomation = conso_input ; 
    } 
    @Override 
    public String toString(){
        return "car"   this.imatriculation   " "   this.nb_chevaux   " "   this.consomation ; 
    }
}

When compiling I get and error in the toString method because the attributes of vehicle are private.

But using super isn't supposed to fix this problem ?

How can I fix this ? Am I obliged to use getters ?

Thanks!

CodePudding user response:

Private members are only accessible in objects that are that exact class. Subclasses can't access them. The way to think about it is that private members should be used for functionality that only that exact class needs to do; if you want a subclass to do it, it can't be private.

That's where another visibility can come in: protected. Protected fields can be accessed by objects of the class or a subclass. So one solution would be to change your fields in the parent class to

protected String imatriculation; 
protected int nb_chevaux; 
protected double consomation; 

This would be the recommended approach if you expect that you will need to be using this fields quite a bit in your subclass. However, it is often the case that you can encapsulate everything you need them for in the parent class. For instance, here you could refactor your toString() method so that it doesn't need access to those fields, by giving the parent class a public toString() method and calling that, like so:

public abstract class vehicle {
    
    ...

    @Override
    public String toString() {
        return imatriculation   " "   nb_chevaux   " "   consomation;
    }

}

public class car extends vehicle {

    ...

    @Override
    public String toString() {
        return "car "   super.toString();
    }

}

I would recommend this second approach when you are able to avoid using protected fields as it takes advantage of object-oriented design rather than trying to get around it. Circumventing OOP in such an OOP-centric language is asking for a headache.

  • Related