next up previous
Next: Fundamental features of derived Up: Unit 08 Previous: Inheritance

Inheritance: example

Suppose we have defined a class Person as follows:

public class Person {
  private String name;
  private String residence;

  public Person(String n, String r) {   // constructor
    name = n;     residence = r;
  }
  public String getName() {
    return name;
  }
  public String getResidence() {
    return residence;
  }
  public void setResidence(String newResidence) {
    residence = newResidence;
  }
}

We derive the subclass Student from the class Person as follows:

public class Student extends Person {
  private String faculty;

  public Student(...) {   // constructor
    ...
  }

  public String getFaculty() {
    return faculty;
  }
}

The objects of the class Student are characterized by the properties inherited from the class Person and additionally by the faculty at which the student is registered.


next up previous
Next: Fundamental features of derived Up: Unit 08 Previous: Inheritance