When I execute this code, it only returns "Eating" instead of "Studying" AND "Eating." Can anyone explain it?
public class Student {
public String getFood() {
return "Pizza";
}
public String getInfo(){
return "Studying";
}
}
public class GradStudent extends Student {
public String getFood(){
return "Taco";
}
public String getInfo(){
super.getInfo();
return "Eating";
}
public static void main(String[] args){
Student s = new GradStudent();
System.out.println(s.getInfo());
}
}
In the getInfo()
method of the Student
is getting invoked, but currently you are not using the result.
If, in the GradStudent
class, you want to return a String that combines the result of the Student.getInfo
method with the specific value from GradStudent
you could do something like this:
public class GradStudent extends Student {
public String getFood(){
return "Taco";
}
public String getInfo(){
String studentInfo = super.getInfo();
return studentInfo + " Eating";
}
public static void main(String[] args){
Student s = new GradStudent();
System.out.println(s.getInfo());
}
}