Lecture Java™ How to Program (8/e) - Chapter 10: Object-oriented programming: Polymorphism

In this chapter, you will learn: The concept of polymorphism, to use overridden methods to effect polymorphism, to distinguish between abstract and concrete classes, to declare abstract methods to create abstract classes, how polymorphism makes systems extensible and maintainable, to determine an object's type at execution time, to declare and implement interfaces. | Java™ How to Program, 8/e (C) 2010 Pearson Education, Inc. All rights reserved. (C) 2010 Pearson Education, Inc. All rights reserved. (C) 2010 Pearson Education, Inc. All rights reserved. Polymorphism Enables you to “program in the general” rather than “program in the specific.” Polymorphism enables you to write programs that process objects that share the same superclass as if they’re all objects of the superclass; this can simplify programming. (C) 2010 Pearson Education, Inc. All rights reserved. Example: Suppose we create a program that simulates the movement of several types of animals for a biological study. Classes Fish, Frog and Bird represent the three types of animals under investigation. Each class extends superclass Animal, which contains a method move and maintains an animal’s current location as x-y coordinates. Each subclass implements method move. A program maintains an Animal array containing references to objects of the various Animal subclasses. To . | Java™ How to Program, 8/e (C) 2010 Pearson Education, Inc. All rights reserved. (C) 2010 Pearson Education, Inc. All rights reserved. (C) 2010 Pearson Education, Inc. All rights reserved. Polymorphism Enables you to “program in the general” rather than “program in the specific.” Polymorphism enables you to write programs that process objects that share the same superclass as if they’re all objects of the superclass; this can simplify programming. (C) 2010 Pearson Education, Inc. All rights reserved. Example: Suppose we create a program that simulates the movement of several types of animals for a biological study. Classes Fish, Frog and Bird represent the three types of animals under investigation. Each class extends superclass Animal, which contains a method move and maintains an animal’s current location as x-y coordinates. Each subclass implements method move. A program maintains an Animal array containing references to objects of the various Animal subclasses. To simulate the animals’ movements, the program sends each object the same message once per second—namely, move. (C) 2010 Pearson Education, Inc. All rights reserved. Each specific type of Animal responds to a move message in a unique way: a Fish might swim three feet a Frog might jump five feet a Bird might fly ten feet. The program issues the same message (., move) to each animal object, but each object knows how to modify its x-y coordinates appropriately for its specific type of movement. Relying on each object to know how to “do the right thing” in response to the same method call is the key concept of polymorphism. The same message sent to a variety of objects has “many forms” of results—hence the term polymorphism. (C) 2010 Pearson Education, Inc. All rights reserved. With polymorphism, we can design and implement systems that are easily extensible New classes can be added with little or no modification to the general portions of the program, as long as the new classes .

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
105    182    3    22-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.