johnny
johnny

Reputation: 19735

Is it me or is there a typo in this courseware entry?

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;

Upvotes: 1

Views: 349

Answers (3)

Andreas Dolk
Andreas Dolk

Reputation: 114787

unless the three-dot-area contains something like

private String String = "Alice";

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

Upvotes: 2

notnoop
notnoop

Reputation: 59299

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.

Upvotes: 3

glmxndr
glmxndr

Reputation: 46586

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

Upvotes: 6

Related Questions