Chapter 14 Inheritance and Polymorphism 2000 McGraw-Hl‖ Introduction to Object-Oriented Programming with Java-Wu Chapter 14-1
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 14 - 1 Chapter 14 Inheritance and Polymorphism
Chapter 14 objectives After you have read and studied this chapter, you should be able to e Write programs that are easily extensible and modifiable by applying polymorphism in program design e Define reusable classes based on inheritance and abstract classes and abstract methods e Define methods using the protected modifier. e Parse strings using a String Tokenizer object C 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 14-2
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 14 - 2 Chapter 14 Objectives After you have read and studied this chapter, you should be able to Write programs that are easily extensible and modifiable by applying polymorphism in program design. Define reusable classes based on inheritance and abstract classes and abstract methods. Define methods using the protected modifier. Parse strings using a StringTokenizer object
Defining classes with Inheritance r We introduced the concept of inheritance in Chapter 1 r We will present a concrete example of using an inheritance in java to define related classes Suppose we want to model graduate and undergraduate students in maintaining a class roster. r For both types of students we keep their name, three test scores and the final course grade. The formula for deriving the final course grades are different for undergraduate and graduate students r How shall we implement the two types of students? C 2000 McGraw-Hill troduction to Object-Oriented Programming with Java--Wu Chapter 14-3
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 14 - 3 Defining Classes with Inheritance We introduced the concept of inheritance in Chapter 1. We will present a concrete example of using an inheritance in Java to define related classes. Suppose we want to model graduate and undergraduate students in maintaining a class roster. For both types of students we keep their name, three test scores, and the final course grade. The formula for deriving the final course grades are different for undergraduate and graduate students. How shall we implement the two types of students?
Student with two subclasses Student NUM OF TESTS We will define three classes Student Student course Grade Graduate student Undergraduate Student setTestscore name getTestScore Implementation of compute Course Grade is unique to each subclass Graduate Student Undergraduate Student compute CourseGrade compute Course Grade C 2000 McGraw-Hill troduction to Object-Oriented Programming with Java--Wu Chapter 14-4
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 14 - 4 Student with Two Subclasses GraduateStudent computeCourseGrade UndergraduateStudent computeCourseGrade Student Student setTestScore getTestScore NUM_OF_TESTS 3 courseGrade name test[ ] … We will define three classes: Student GraduateStudent UndergraduateStudent Implementation of computeCourseGrade is unique to each subclass
Using polymorphism Effectively r Polymorphism is a feature that allows the rogrammer to send the same message to objects om diffe erent classes r Polymorphism allows the same variable to refer to objects from different(but related) classes Student studenti This will call the student new Graduatestudent()i method of Graduate student student. computeCourseGrade( )i Student student This will call the student new UndergraduateStudent()i method of Undergraduate Student student. compute CourseGrade( )i C 2000 McGraw-Hill troduction to Object-Oriented Programming with Java--Wu Chapter 14-5
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 14 - 5 Using Polymorphism Effectively Polymorphism is a feature that allows the programmer to send the same message to objects from different classes. Polymorphism allows the same variable to refer to objects from different (but related) classes. Student student; student = new GraduateStudent(); student.computeCourseGrade( ); Student student; student = new UndergraduateStudent(); student.computeCourseGrade( ); This will call the method of GraduateStudent This will call the method of UndergraduateStudent
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
Inheritance and member accessibility r Which data members and Superclass methods of a superclass are accessible from the methods of its subclasses? r There is a third modifier called protected in addition to public and private Subclass public private protected C 2000 McGraw-Hill troduction to Object-Oriented Programming with Java--Wu Chapter 14-8
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 14 - 8 Inheritance and Member Accessibility Which data members and methods of a superclass are accessible from the methods of its subclasses? There is a third modifier called protected in addition to public and private. public private protected Superclass Subclass
Access from the subclass methods r Only the private elements of the superclass are not accessible from its subclasses. All other types of elements are accessible my Sub Subclass Superclass accessible X inaccessible C 2000 McGraw-Hill troduction to Object-Oriented Programming with Java--Wu Chapter 14-9
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 14 - 9 Access from the Subclass Methods Only the private elements of the superclass are not accessible from its subclasses. All other types of elements are accessible. mySub Subclass Superclass inaccessible accessible
Access from the outside r Only. the public elements are accessible from the outside objects. mySuper Superclass Sub Subclass Client Superclass xX C 2000 McGraw-Hill troduction to Object-Oriented Programming with Java--Wu Chapter 14-10
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 14 - 10 Access from the Outside Only the public elements are accessible from the outside objects. mySub Subclass Superclass mySuper Superclass Client test