next up previous
Next: Compatibility Up: Unit 08 Previous: Use of super()

Inherited methods and variables

From what we have said, all the objects of the class Student, besides having the proper methods and instance variables defined in Student, inherit all methods and instance variables of Person. For example, we can write:

public class TestStudent {
  public static void main(String[] args) {
    Person p = new Person("Daniele", "Roma");
    System.out.println(p.getName());
    System.out.println(p.getResidence());
    Student s = new Student("Jacopo", "Roma", "Engineering");
    System.out.println(s.getName());       // OK! method inherited from Person
    System.out.println(s.getResidence());  // OK! method inherited from Person
    System.out.println(s.getFaculty());    // OK! method defined in Student
  }
}

The methods getName() and getResidence(), inherited from Person are effectively methods of the class Student.


next up previous
Next: Compatibility Up: Unit 08 Previous: Use of super()