I took this from an online MIT courseware discussion (pdf warning):
public class Human {
private String name;
...
public Human(String name) {
this.name = name;
}
public String getName() {
return String;
}
}
public class Student extends Human {
private String username;
public Student(String name, String username) {
super(name);
this.username = username;
}
public String getName() {
return username;
}
public String getRealName() {
return super.getName();
}
}
...
public class World {
...
void someMethod() {
Student alice = new Student("Alice", "abc");
System.out.println(alice.getRealName()); // what gets printed?
Why does getRealName return anything. I know it returns Alice because the constructor is called by super(name) but my question is about:
return String;
Why doesn't getName in the Human class have to be
return name;
Thank you.
edit: thank you. I just wanted to be sure it wasn't something I didn't know about.