//pickup your quiz, midterm leftover, etc //I'll post some sample questions for exam soon //recall the Student class and its 2 subclasses: Student s = new Student("Pam", 8923423); s.toString() StudentAthlete s1 = new StudentAthlete("Joe", 98023423, "Swimming") s1.toString() s1.getSport() StudentNerd s2 = new StudentNerd("Joe", 98023423, 25); s2.toString() s2.getSprot() //ooops! s2 is a StudentNerd, it has no knowlege of StudentAthlete class s2.readBook() s2.toString() s1.readBook() //oops! s1 is a StudentAthlete , it has no knowlege of StudentNerd class s.readBook() //oops! what's the problem? s.getSport() //oops! what's the problem? //here is the class hierarchy: Object | Student | / \ StudentAthlete StudentNerd Student s3 s3 = s1 //remember s1 (athelte) is a student s3.toString() s3.getSprot() s3 = s2 //remember s2 (nerd) is a student s3.toString() s3.readBook() StudnetNerd s4 s4 = s1 //oops! s1 is athlete which is not nerd so cannot assign!! StudnetNerd s5 s5 = s2 //oops! s2 nerd is which is not athlete so cannot assign!! //How do we know what object s refers to? we use isntanceof operator: s1 instanceof Object s1 instanceof Student s1 instanceof StudentAthlete s2 instanceof Object s2 instanceof Student s2 instanceof StudentAthlete Student s = s2 //recall s2 is athlete // apparent type of s is Student // real type of s is StudentAthlete //you can cast it back to StudentAthlete to access the extra methods: StudentAthlete s6 = (StudentAthlete) s; s6.getSport() Student s = new Student("David", 8783274); StudentAthlete s4 = (StudentAthlete) s; //oops!! // Can't cast a Student to type StudentAthlete. There are // more methods/data in StudentAthlete than there are in Student. // We can cast a StudentAthlete to type Student. We just ignore // the additional data/methods. //also we cannot cast a StudentNerd into StudentAthlete! Student[] a = new Student[3] a[0] = new Student("s1",1000) a[1] = new StudentAthlete("s2",2000,"Swimming") a[2] = new StudentNerd("s3",3000,8) a[i].toString() a[i].getSport()!! //let's write OO class, to see what compiles and what doesn't