当前位置:高等教育资讯网  >  中国高校课件下载中心  >  大学文库  >  浏览文档

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

资源类别:文库,文档格式:PPT,文档页数:76,文件大小:495KB,团购合买
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).
点击下载完整版文档(PPT)

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

点击下载完整版文档(PPT)VIP每日下载上限内不扣除下载券和下载次数;
按次数下载不扣除下载券;
24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
共76页,可试读20页,点击继续阅读 ↓↓
相关文档

关于我们|帮助中心|下载说明|相关软件|意见反馈|联系我们

Copyright © 2008-现在 cucdc.com 高等教育资讯网 版权所有