Lecter Java: Program design - Chapter 9: Inheritance and polymorphism

Inheritance and polymorphism are two powerful mechanisms of object-oriented languages. Inheritance is the ability to define a new class using an existing class as a basis. Polymorphism is a programming language behavior where the same code expression can invoke different methods depending on the type of objects using the code. In this chapter, we begin our introduction of these two mechanisms by extending the standard class Point. | Inheritance and Polymorphism Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Inheritance Organizes objects in a top-down fashion from most general to least general Inheritance defines a “is-a” relationship A mountain bike “is a” kind of bicycle A SUV “is a” kind of automobile A border collie “is a” kind of dog A laptop “is a” kind of computer Musical instrument hierarchy Idiophones: Solid instruments that are intrinsically sonorous, ., bell and cymbal; Membranophones: Instruments with taut membranes, ., drum and kazoo; Aerophones: Instruments that enclose and free masses of air, ., clarinet and whistle; Chordophones: Instruments with stretched strings, ., lyre and violin; Electrophones: Instruments with oscillating electric circuits, . synthesizer and electric piano. Musical instrument hierarchy The hierarchy helps us understand the relationships and similarities of musical instruments A clarinet “is a” kind of reeded . | Inheritance and Polymorphism Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Inheritance Organizes objects in a top-down fashion from most general to least general Inheritance defines a “is-a” relationship A mountain bike “is a” kind of bicycle A SUV “is a” kind of automobile A border collie “is a” kind of dog A laptop “is a” kind of computer Musical instrument hierarchy Idiophones: Solid instruments that are intrinsically sonorous, ., bell and cymbal; Membranophones: Instruments with taut membranes, ., drum and kazoo; Aerophones: Instruments that enclose and free masses of air, ., clarinet and whistle; Chordophones: Instruments with stretched strings, ., lyre and violin; Electrophones: Instruments with oscillating electric circuits, . synthesizer and electric piano. Musical instrument hierarchy The hierarchy helps us understand the relationships and similarities of musical instruments A clarinet “is a” kind of reeded instrument Reeded instruments “are a” kind of aerophone The “is-a” relationship is transitive A clarinet “is a” kind of reeded instrument A reeded instrument “is a” kind of aerophone A clarinet “is a” kind of aerophone Object-oriented terminology In object-oriented programming languages, a class created by extending another class is called a subclass The class used for the basis is called the superclass Alternative terminology The superclass is also referred to as the base class The subclass is also referred to as the derived class ThreeDimensionalPoint Build a new class ThreeDimensionalPoint using inheritance ThreeDimensionalPoint extends the awt class Point Point is the superclass (base class) ThreeDimensionalPoint is the subclass (derived class) ThreedimensionalPoint extends Point by adding a new property to Point—a z-coordinate y-axis x-axis z-axis (x, y, z) Class ThreeDimensionalPoint package geometry; import .*; public class ThreeDimensionalPoint extends Point { // private class

Không thể tạo bản xem trước, hãy bấm tải xuống
TỪ KHÓA LIÊN QUAN
TÀI LIỆU MỚI ĐĂNG
346    2    1    23-05-2024
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.