《JAVA OOP开发》英文版 Chapter 14 Inheritance and Polymorphism

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
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 《JAVA OOP开发》英文版 Chapter 13 GUI Objects and Event-Driven Programming.ppt
- 《JAVA OOP开发》英文版 Chapter 12 Reusable classes and packages.ppt
- 《JAVA OOP开发》英文版 Chapter 11 File Input and Output.ppt
- 《JAVA OOP开发》英文版 Chapter 10 Sorting and Searching.ppt
- 《JAVA OOP开发》英文版 Chapter 9 objectives.ppt
- 《JAVA OOP开发》英文版 Chapter 8 Characters and strings.ppt
- 《JAVA OOP开发》英文版 Chapter 7 Repetition Statements.ppt
- 《JAVA OOP开发》英文版 Chapter 6 Selection statements.ppt
- 《JAVA OOP开发》英文版 Chapter 5 Processing Input with Applets.ppt
- 《JAVA OOP开发》英文版 Chapter 4 Defining Instantiable Classes.ppt
- 《JAVA OOP开发》英文版 Chapter 3 Numerical Data.ppt
- 《JAVA OOP开发》英文版 Chapter 2 Java Programming Basics.ppt
- 《JAVA OOP开发》英文版 Chapter 1 Introduction to Object-oriented Programming and Software Development.ppt
- 《JAVA OOP开发》英文版 Introduction to Computers and Programming Languages.ppt
- 《Windows DNA应用程式》 面向对象分析与设计讲义.ppt
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(PPT课件讲稿)第四章 第四讲 分支结构.ppt
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(PPT课件讲稿)第十二章 文件.ppt
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(PPT课件讲稿)第十一章 复杂数据类型及排序.ppt
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(教案讲义)第六讲 数组.doc
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(PPT课件讲稿)第六讲 数组.ppt
- 《JAVA OOP开发》英文版 Chapter 15 Case Study Class Roster Maintenance program.ppt
- 《JAVA OOP开发》英文版 Chapter 16 Chapter 16 Recursive algorithms.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第一章 绪论.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第七章 牛行接与应用.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第三章 MCS-51单片机指令系统.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第九章 A/D、D/A转换接口.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第二章 MCS-51学机组成理.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第五章 输入/输出与中断.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第八章 并行接口与应用.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第六章 定时器/计数器及应用.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第十章 单片机应用系统设计与开发.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第四章 MCS-51单片机存储器的扩展.ppt
- 《网络安全设计》 第一章 安全设计简介.ppt
- 《网络安全设计》 第二章 创建网络安全计划.ppt
- 《网络安全设计》 第三章 确定网络安全威胁.ppt
- 《网络安全设计》 第四章 分析安全风险.ppt
- 《网络安全设计》 第五章 创建物理资源安全设计.ppt
- 《网络安全设计》 第六章 创建计算机安全设计.ppt
- 《网络安全设计》 第七章 创建账户安全设计.ppt
- 《网络安全设计》 第八章 身份验证的安全设计.ppt