Creating the roster array Student roster[]= new Student[40]i roster is an array of Student objects roster[o]= new Graduatestudent()i Create instances of roster[l]= new Undergraduatestudent()i he subclasses of oster [2]= new Undergraduatestudent()i Student roster [3]= new Graduatestudent()i roster 234 36373839 Graduate Under Under- Graduate Student graduate graduate-Student Student Student C 2000 McGraw-Hill troduction to Object-Oriented Programming with Java--Wu Chapter 14-6
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 14 - 6 Creating the roster Array Student roster[ ] = new Student[40]; roster is an array of Student objects. Create instances of the subclasses of Student. roster[0] = new GraduateStudent( ); roster[1] = new UndergraduateStudent( ); roster[2] = new UndergraduateStudent( ); roster[3] = new GraduateStudent( ); 0 1 2 3 4 36 37 38 39 roster GraduateStudent UndergraduateStudent UndergraduateStudent GraduateStudent
Processing the roster Array for (int i =0; I< numberofstudents; 1++ Notice how the roster[i]. computeCourseGrade() polymorphism is used here int undergradcount =0; for (int i =0; i< numberofstudents; 1++ Use instance of to f( roster[i] instanceof determine the class UndergraduateStudent ) i the object belongs to undergradcount++i C 2000 McGraw-Hill troduction to Object-Oriented Programming with Java--Wu Chapter 14-7
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 14 - 7 Processing the roster Array for (int i = 0; I < numberOfStudents; i++ ) { roster[i].computeCourseGrade( ); } Use instanceOf to determine the class the object belongs to. int undergradCount = 0; for (int i = 0; i < numberOfStudents; i++ ) { if ( roster[i] instanceOf UndergraduateStudent ) { undergradCount++; } } Notice how the polymorphism is used here