I have a class Person and a class Instructor that extends Person
public class Driver {
public static void main(String args[]) {
Person[] array = new Person[5];
array[1] = new Person("John Doe");
array[2] = new Person("Bobby Gram");
array[3] = new Person("Jeb Too");
array[4] = new Instructor("Jill Crill", "Computer Science");
array[5] = new Instructor("Hello World", "Math");
for(int i = 0; i < array.length; i++) {
System.out.println(array[i].toString());
}
for(int i = 0; i < array.length; i++) {
int total = 0;
if (array[i] instanceof Instructor) {
Person person = (Instructor) array[i];
person.getSubject();
}
}
}
}
for(int i = 0; i < array.length; i++) {
int total = 0;
if (array[i] instanceof Instructor) {
Person person = (Instructor) array[i];
person.getSubject(); //error: The method getSubject() is undefined for the type Person
}
}
It's giving me an error even though I downcasted? The Instructor class has a getSubject() method.