tags:

views:

120

answers:

3

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.

+6  A: 

It should be. It's a typo. This code as you have pasted it would not compile.

subtenante
I guess I got bamboozled by it coming from MIT so I figured it must be me that's wrong and not them.
johnny
+3  A: 

You are correct. It's a typo and should be return name.

Please notify the instructor, or the contact person for the class, so they can update the pdf.

notnoop
A: 

unless the three-dot-area contains something like

private String String = "Alice";

but, nay, I guess it's a typo ;-)

Andreas_D