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