I have the following problem with displaying an object as a string:
class Gast{
String voorNaam, achterNaam;
datum geboorteDatum;
Gast(String vNaam, String aNaam, datum input){
voorNaam = vNaam;
achterNaam = aNaam;
geboorteDatum = input;
}
public String toString(){
return("Naam: " + voorNaam + " " + achterNaam + " " + geboorteDatum);
}
}
here I implemented a way to represent this object in a string, however when I try to use that in this class:
class Kamer{
boolean vrij;
Gast gast;
Kamer(){}
public String toString(){
if(vrij == true){
return "De kamer is vrij!";
}
else{
return gast;
}
}
}
I get the following error:
kamer.java:17: error: incompatible types: Gast cannot be converted to String
return gast;
I gave a string representation of the object Gast in its class? Does the other class not inherit the representation I gave?
Gastis not a string. You have to callgast.toString().