// filename RollCall.java // // example of upcasting, polymorphism, interface // // output will be // Joe is present! // Tony is present! // Jeremy is present! import java.util.*; // needed for Vector // inteface Person, forces class to implement getName method interface Person { String getName(); // force any class to implement this method } // class Student implements Person class Student implements Person { String name; Student(String n) { // constructor name = n; } public String getName() { // implementation of getName from Person interface return name; } } // class RollCall, test drive interface Person and class Student // upcasting Student to Person is ok because a Student is a Person // polymorphism behavior, person is retrieved from the Vector upcast as a Student // when person.getName() method is called, the correct name is printed public class RollCall { public static void main(String[] args) { Vector classRoom = new Vector(); // add students in the class room classRoom.addElement(new Student("Joe")); classRoom.addElement(new Student("Tony")); classRoom.addElement(new Student("Jeremy")); // lets find out who's in the class romm for (int i = 0; i < classRoom.size(); i++) { Person person = (Student)classRoom.elementAt(i); // upcasting Student to Person System.out.println(person.getName() + " is present!"); // polymorphism behavior } } }