Lecture Java: Chapter 9
Số trang: 31
Loại file: pptx
Dung lượng: 655.28 KB
Lượt xem: 13
Lượt tải: 0
Xem trước 4 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
Lecture Java: Chapter 9 focuses on deriving new classes from existing classes, the protected modifier, creating class hierarchies, abstract classes, indirect visibility of, inherited members, Overloading.
Nội dung trích xuất từ tài liệu:
Lecture Java: Chapter 9 Chapter 9Inheritance Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William LoftusCopyright © 2012 Pearson Education, Inc.Inheritance• Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes• Chapter 9 focuses on: – deriving new classes from existing classes – the protected modifier – creating class hierarchies – abstract classes – indirect visibility of inherited members – Overloading 8-2Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance and Visibility Designing for Inheritance 8-3Inheritance• Inheritance allows a software developer to derive a new class from an existing one• The existing class is called the parent class, or superclass, or base class• The derived class is called the child class or subclass• As the name implies, the child inherits characteristics of the parent• That is, the child class inherits the methods and data defined by the parent class 8-4Inheritance • Inheritance relationships are shown in a UML class diagram using a solid arrow with an unfilled triangular arrowhead pointing to the parent class Vehicle Car • Proper inheritance creates an is-a relationship, meaning the child is a more specific version of the parent 8-5Inheritance• A programmer can tailor a derived class as needed by adding new variables or methods, or by modifying the inherited ones• Software reuse is a fundamental benefit of inheritance• By using existing software components to create new ones, we capitalize on all the effort that went into the design, implementation, and testing of the existing software 8-6Deriving Subclasses • In Java, we use the reserved word extends to establish an inheritance relationship class Car extends Vehicle { // class contents } • See Words.java (page 382) • See Book.java (page 385) • See Dictionary.java (page 386) 8-7The protected Modifier • Visibility modifiers affect the way that class members can be used in a child class • Variables and methods declared with private visibility cannot be referenced by name in a child class • They can be referenced in the child class if they are declared with public visibility -- but public variables violate the principle of encapsulation • There is a third visibility modifier that helps in inheritance situations: protected 8-8The protected Modifier • The protected modifier allows a child class to reference a variable or method directly in the child class • It provides more encapsulation than public visibility, but is not as tightly encapsulated as private visibility • A protected variable is visible to any class in the same package as the parent class • The details of all Java modifiers are discussed in Appendix E • Protected variables and methods can be shown with a # symbol preceding them in UML 8-9 diagramsClass Diagram for Words(p 386) Book # pages : int + pageMessage() : void Words Dictionary - definitions : int + main (args : String[]) : void + definitionMessage() : void 8-10The super Reference• Constructors are not inherited, even though they have public visibility• Yet we often want to use the parents constructor to set up the parents part of the object• The super reference can be used to refer to the parent class, and often is used to invoke the parents constructor• See Words2.java (page 445)• See Book2.java (page 446)• See Dictionary2.java (pag ...
Nội dung trích xuất từ tài liệu:
Lecture Java: Chapter 9 Chapter 9Inheritance Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William LoftusCopyright © 2012 Pearson Education, Inc.Inheritance• Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes• Chapter 9 focuses on: – deriving new classes from existing classes – the protected modifier – creating class hierarchies – abstract classes – indirect visibility of inherited members – Overloading 8-2Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance and Visibility Designing for Inheritance 8-3Inheritance• Inheritance allows a software developer to derive a new class from an existing one• The existing class is called the parent class, or superclass, or base class• The derived class is called the child class or subclass• As the name implies, the child inherits characteristics of the parent• That is, the child class inherits the methods and data defined by the parent class 8-4Inheritance • Inheritance relationships are shown in a UML class diagram using a solid arrow with an unfilled triangular arrowhead pointing to the parent class Vehicle Car • Proper inheritance creates an is-a relationship, meaning the child is a more specific version of the parent 8-5Inheritance• A programmer can tailor a derived class as needed by adding new variables or methods, or by modifying the inherited ones• Software reuse is a fundamental benefit of inheritance• By using existing software components to create new ones, we capitalize on all the effort that went into the design, implementation, and testing of the existing software 8-6Deriving Subclasses • In Java, we use the reserved word extends to establish an inheritance relationship class Car extends Vehicle { // class contents } • See Words.java (page 382) • See Book.java (page 385) • See Dictionary.java (page 386) 8-7The protected Modifier • Visibility modifiers affect the way that class members can be used in a child class • Variables and methods declared with private visibility cannot be referenced by name in a child class • They can be referenced in the child class if they are declared with public visibility -- but public variables violate the principle of encapsulation • There is a third visibility modifier that helps in inheritance situations: protected 8-8The protected Modifier • The protected modifier allows a child class to reference a variable or method directly in the child class • It provides more encapsulation than public visibility, but is not as tightly encapsulated as private visibility • A protected variable is visible to any class in the same package as the parent class • The details of all Java modifiers are discussed in Appendix E • Protected variables and methods can be shown with a # symbol preceding them in UML 8-9 diagramsClass Diagram for Words(p 386) Book # pages : int + pageMessage() : void Words Dictionary - definitions : int + main (args : String[]) : void + definitionMessage() : void 8-10The super Reference• Constructors are not inherited, even though they have public visibility• Yet we often want to use the parents constructor to set up the parents part of the object• The super reference can be used to refer to the parent class, and often is used to invoke the parents constructor• See Words2.java (page 445)• See Book2.java (page 446)• See Dictionary2.java (pag ...
Tìm kiếm theo từ khóa liên quan:
Kỹ thuật lập trình Lecture Java Chapter 9 Lập trình Java Lập trình Mobile Ngôn ngữ lập trình Công nghệ thông tinGợi ý tài liệu liên quan:
-
52 trang 426 1 0
-
Top 10 mẹo 'đơn giản nhưng hữu ích' trong nhiếp ảnh
11 trang 309 0 0 -
74 trang 293 0 0
-
96 trang 289 0 0
-
Báo cáo thực tập thực tế: Nghiên cứu và xây dựng website bằng Wordpress
24 trang 288 0 0 -
Đồ án tốt nghiệp: Xây dựng ứng dụng di động android quản lý khách hàng cắt tóc
81 trang 276 0 0 -
Giáo trình Lập trình hướng đối tượng: Phần 2
154 trang 271 0 0 -
EBay - Internet và câu chuyện thần kỳ: Phần 1
143 trang 270 0 0 -
Tài liệu dạy học môn Tin học trong chương trình đào tạo trình độ cao đẳng
348 trang 269 1 0 -
Bài thuyết trình Ngôn ngữ lập trình: Hệ điều hành Window Mobile
30 trang 261 0 0