复旦大学:《程序设计》课程教学资源(PPT课件)Chapter 8 Inheritance and Polymorphism

Chapter 8 Inheritance and Polymorphism Prerequisites for Part Il er 5 Arrays hapter 6 Objects and classes hapter7 St You can cover GUI after Chapter 8 Chapter 8 Inheritance and Polymorphism >Chapter 11 Getting Started with GUI Programming Chapter 9 Abstract Classes and Interfaces Chapter 12 Event-Driven Programmin Chapter 10 Object-Oriented Modelin 切皆有缘起 Chapter 15 Exceptions and Assertions You can cover Exceptions and 1O after Chapter 8 hapter 16 Simple Input and Output Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 8 Inheritance and Polymorphism Prerequisites for Part II Chapter 6 Objects and Classes Chapter 7 Strings Chapter 8 Inheritance and Polymorphism Chapter 5 Arrays Chapter 9 Abstract Classes and Interfaces Chapter 10 Object-Oriented Modeling Chapter 11 Getting Started with GUI Programming Chapter 12 Event-Driven Programming Chapter 15 Exceptions and Assertions Chapter 16 Simple Input and Output You can cover Exceptions and I/O after Chapter 8 You can cover GUI after Chapter 8 一切皆有缘起

Objectives To develop a subclass from a superclass through inheritance ($8.2) To invoke the superclass constructors and methods using the super keyword (§8.3) To override methods in the subclass(88.4) To explore the useful methods (equals(objectl, hash Codeo, toString@, finalized clone(, and getClassQ)in the Object class($8.,88. 11 Optional To comprehend polymorphism, dynamic binding, and generic programming (§86) To describe casting and explain why explicit downcasting is necessary($8.7) To understand the effect of hiding data fields and static methods($8.8 Optional) To restrict access to data and methods using the protected visibility modifier (§8.9) To declare constants, unmodifiable methods, and nonextendable class using the final modifier($8.10) To initialize data using initial ization blocks and distinguish between instance initialization and static initial ization blocks(s8.12 Optioanl) Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 2 Objectives To develop a subclass from a superclass through inheritance (§8.2). To invoke the superclass’s constructors and methods using the super keyword (§8.3). To override methods in the subclass (§8.4). To explore the useful methods (equals(Object), hashCode(), toString(), finalize(), clone(), and getClass()) in the Object class (§8.5, §8.11 Optional). To comprehend polymorphism, dynamic binding, and generic programming (§8.6). To describe casting and explain why explicit downcasting is necessary (§8.7). To understand the effect of hiding data fields and static methods (§8.8 Optional). To restrict access to data and methods using the protected visibility modifier (§8.9). To declare constants, unmodifiable methods, and nonextendable class using the final modifier (§8.10). To initialize data using initialization blocks and distinguish between instance initialization and static initialization blocks (§8.12 Optioanl)

Introduction o Object-oriented programming ○ Inheritance o Software reusability o Classes are created from existing ones Absorbing attributes and behaviors Adding new capabilities pOlymorphism o Enables developers to write programs in general fashion o Helps add new capabilities to system Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 3 Introduction ⚫Object-oriented programming Inheritance ⚫Software reusability ⚫Classes are created from existing ones • Absorbing attributes and behaviors • Adding new capabilities Polymorphism ⚫Enables developers to write programs in general fashion ⚫Helps add new capabilities to system

Introduction(cont o Object-oriented programming ○ Inheritance e Subclass inherits from superclass Subclass usually adds instance variables and methods o Single vS. multiple inheritance Java does not support multiple inheritance Interfaces(discussed later)achieve the same effect o“Isa” relationship COmposition “Hasa” relationship Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 4 Introduction (cont.) ⚫Object-oriented programming Inheritance ⚫Subclassinherits from superclass • Subclass usually adds instance variables and methods ⚫Single vs. multiple inheritance • Java does not support multiple inheritance Interfaces (discussed later) achieve the same effect ⚫“Is a” relationship Composition ⚫“Has a” relationship

Object relationships Composition Whole-part -Existance of an object relies on another VideoStore Customer VideoRented Transaction# Name Date VideoRented Videoltem Videoltem Stock Title ating Liang, Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 5 Object relationships Composition •Whole-part –Existance of an object relies on another

Superclasses and subclasses Is a?" Relationship oObject"is an object of another class o Rectangle"is a quadrilateral Class Rectangle inherits from class Quadrilateral O Form tree-like hierarchical structures hyperclass Subc lasses Student Graduatestudent UndergraduateStudent Shape Circle Triangle Rectangle L。an Carload HomeImprovementLoan M。 tracer。ar Employee Facul tyMember sta£ Member ACcount CheckingAccount SavingsAccount ig.9.1 Some simple inheritanc e examp les in whic h the subc lass" is a"superclass
Liang,Introduction to Java Programming,revised by Dai-kaiyu 6 Superclasses and Subclasses ⚫“Is a” Relationship Object “is an” object of another class ⚫Rectangle “is a” quadrilateral • Class Rectangle inherits from class Quadrilateral Form tree-like hierarchical structures

Superclasses and Subclasses Superclass Circle Circle methods Circle data Inheritance Subclass Cylinder Circle methods Circle data Cylinder Methods Cylinder Data uperclass class UML Diagram Circle Cylinder radius length getRadius atRadius FgetLength find Area length Find Volume Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 7 Superclasses and Subclasses Superclass Circle Circle Methods Circle Data Inheritance Cylinder Circle Methods Cylinder Methods Circle Data Cylinder Data Subclass Circle -radius +getRadius +setRadius +findArea Cylinder -length +getLength +setLength +findVolume Superclass Subclass UML Diagram

//Cvlinder iava: Class definition for describing cvlinder public class Cylinder extends Circle i private double length =1; S Subclass supertype /* Return length */ subtype Circle Cylinder public double getLengtho i radius return length length getRadius setRadius getLength finda length find volume /* Set length */ public void setlength( double length)t this length= length; /* Return the volume of this cylinder * public double find volumed i return findareao* length; Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 8 // Cylinder.java: Class definition for describing Cylinder public class Cylinder extends Circle { private double length = 1; /** Return length */ public double getLength() { return length; } /** Set length */ public void setLength(double length) { this.length = length; } /** Return the volume of this cylinder */ public double findVolume() { return findArea() * length; } } Circle -radius +getRadius +setRadius +findArea Cylinder -length +getLength +setLength +findVolume Superclass Subclass supertype subtype

Cylinder cylinder=new Cylinder System. out printIn("The length is"+ cylinder. getLengthO) System. out println(" The radius is"+ cylinder. getRadiuso) System. out println("The volume of the cylinder is"+ cylinder. find Volume) ystem out printIn("The area of the circle is"+ cylinder. find Areao) Pravate data fields and mothods will not The output is be inherited in a subclass The length is 1.0 The radius is 1.0 The volume of the cylinder is 3.14159 The area of the circle is 3.14159 Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 9 Cylinder cylinder = new Cylinder(); System.out.println("The length is " + cylinder.getLength()); System.out.println("The radius is " + cylinder.getRadius()); System.out.println("The volume of the cylinder is " + cylinder.findVolume()); System.out.println("The area of the circle is " + cylinder.findArea()); The length is 1.0 The radius is 1.0 The volume of the cylinder is 3.14159 The area of the circle is 3.14159 The output is Pravate data fields and mothods will not be inherited in a subclass

An inheritance hierarchy for university Community Members. CommunityMember is a direct superclass of Employee tRainer Communi tyMember is Faculty Employee Strat Alumi Administrator Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 10 An inheritance hierarchy for university CommunityMembers. CommunityMember Employee Student Faculty Staff Administrator Teacher Alumni CommunityMember is a direct superclass of Employee CommunityMemberis an indirect superclass of Faculty
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 复旦大学:《程序设计》课程教学资源(PPT课件)Chapter 7 String.ppt
- 复旦大学:《程序设计》课程教学资源(PPT课件)Chapter 6 Objects and Classes.ppt
- 复旦大学:《程序设计》课程教学资源(PPT课件)Chapter 5 Arrays.ppt
- 复旦大学:《程序设计》课程教学资源(PPT课件)Chapter 4 Methods.ppt
- 复旦大学:《程序设计》课程教学资源(PPT课件)Chapter 3 Control Statements.ppt
- 复旦大学:《程序设计》课程教学资源(PPT课件)Chapter 2 Primitive Data Types and Operations.ppt
- 复旦大学:《程序设计》课程教学资源(PPT课件)Chapter 1 Introduction to Computers, Programs, and Java.ppt
- 复旦大学:《程序设计》课程教学资源(PPT课件)Chapter 0 course intro Programming Language(Using Java).ppt
- 复旦大学:《程序设计》课程教学资源(Java Lab)Game of Life_题目要求.pdf
- 复旦大学:《程序设计》课程教学资源(Java Lab)Game of Life.ppt
- 复旦大学:《程序设计》课程教学资源(Java Lab)验证哥德巴赫猜想.ppt
- 复旦大学:《程序设计》课程教学资源(Java Lab)Ansi Temple.ppt
- 复旦大学:《程序设计》课程教学资源(Java Lab)富人Smith的生日 If else Switch.ppt
- 复旦大学:《程序设计》课程教学资源(Java Lab)3.ppt
- 复旦大学:《程序设计》课程教学资源(Java Lab)环境配置与第一个程序的运行.ppt
- 复旦大学:《程序设计》课程教学资源(Java Lab)第一个java程序.ppt
- 《高级Web技术》参考资料:语义Web课件 A Semantic Web Primer_Chapter8 Conclusion and Outlook.ppt
- 《高级Web技术》参考资料:语义Web课件 A Semantic Web Primer_Chapter7 Ontology Engineering.ppt
- 《高级Web技术》参考资料:语义Web课件 A Semantic Web Primer_Chapter6 Applications.ppt
- 《高级Web技术》参考资料:语义Web课件 A Semantic Web Primer_Chapter5 Logic and Inference:Rules.ppt
- 复旦大学:《程序设计》课程教学资源(PPT课件)Chapter 9 Abstract Classes and Interfaces.ppt
- 复旦大学:《程序设计》课程教学资源(PPT课件)Chapter 10 Object-Oriented Modeling(oom).ppt
- 复旦大学:《程序设计》课程教学资源(PPT课件)Chapter 11 Getting Started with GUI Programming.ppt
- 复旦大学:《程序设计》课程教学资源(PPT课件)Chapter 12 Event-Driven Programming.ppt
- 复旦大学:《程序设计》课程教学资源(PPT课件)Chapter 13 Creating User Interfaces.ppt
- 复旦大学:《程序设计》课程教学资源(PPT课件)Chapter 14 Applets, Images, and Audio.ppt
- 复旦大学:《程序设计》课程教学资源(PPT课件)Chapter 15 Exceptions and Assertions.ppt
- 复旦大学:《程序设计》课程教学资源(PPT课件)Chapter 16 Simple Input and Output.ppt
- 复旦大学:《信息安全》教学课件_01 Classical Encryption Techniques.pdf
- 复旦大学:《信息安全》教学课件_02 Classical Encryption Techniques(cont.).pdf
- 复旦大学:《信息安全》教学课件_03 Modern Block Ciphers.pdf
- 复旦大学:《信息安全》教学课件_04 Public Key Cryptography, RSA.pdf
- 复旦大学:《信息安全》教学课件_05 Message authentication and Hash function.pdf
- 复旦大学:《信息安全》教学课件_06 The Intro to Information Security.pdf
- 复旦大学:《信息安全》教学课件_07-08 Public Key Infrastructure(PKI)公钥基础设施——公钥技术的应用.pdf
- 复旦大学:《信息安全》教学课件_09 Authentication and supplements.pdf
- 复旦大学:《信息安全》教学课件_10 Authentication Kerberos.pdf
- 复旦大学:《信息安全》教学课件_11.1 IP Security.pdf
- 复旦大学:《信息安全》教学课件_11.2 Web & EC Security.pdf
- 复旦大学:《信息安全》教学课件_12-13 Software Security.pdf