Java Reference
In-Depth Information
10.1 Introduction
10.2 Polymorphism Examples
10.3 Demonstrating Polymorphic Behavior
10.4 Abstract Classes and Methods
10.5 Case Study: Payroll System Using
Polymorphism
10.5.1 Abstract Superclass Employee
10.5.2 Concrete Subclass
SalariedEmployee
10.5.3 Concrete Subclass HourlyEmployee
10.5.4 Concrete Subclass
CommissionEmployee
10.5.5 Indirect Concrete Subclass
BasePlusCommissionEmployee
10.5.6 Polymorphic Processing, Operator
instanceof and Downcasting
10.6 Allowed Assignments Between
Superclass and Subclass Variables
10.7 final Methods and Classes
10.8 A Deeper Explanation of Issues with
Calling Methods from Constructors
10.9 Creating and Using Interfaces
10.9.1 Developing a Payable Hierarchy
10.9.2 Interface Payable
10.9.3 Class Invoice
10.9.4 Modifying Class Employee to
Implement Interface Payable
10.9.5 Modifying Class
SalariedEmployee for Use in the
Payable Hierarchy
10.9.6 Using Interface Payable to Process
Invoices and Employees
Polymorphically
10.9.7 Some Common Interfaces of the Java
API
10.10 Java SE 8 Interface Enhancements
10.10.1 default Interface Methods
10.10.2 static Interface Methods
10.10.3 Functional Interfaces
10.11 (Optional) GUI and Graphics Case
Study: Drawing with Polymorphism
10.12 Wrap-Up
Summary | Self-Review Exercises | Answers to Self-Review Exercises |
Exercises | Making a Difference
10.1 Introduction
We continue our study of object-oriented programming by explaining and demonstrating
polymorphism with inheritance hierarchies. Polymorphism enables you to “program in
the general ” rather than “program in the specific .” In particular, polymorphism enables you
to write programs that process objects that share the same superclass, either directly or in-
directly, as if they were all objects of the superclass; this can simplify programming.
Consider the following example of polymorphism. 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 types of animals under investigation. Imagine that each class
extends superclass Animal , which contains a method move and maintains an animal's cur-
rent location as x - y coordinates. Each subclass implements method move . Our program
maintains an Animal array containing references to objects of the various Animal sub-
classes. To simulate the animals' movements, the program sends each object the same mes-
sage once per second—namely, move . Each specific type of Animal responds to a move
message in its own way—a Fish might swim three feet, a Frog might jump five feet and a
Bird might fly ten feet. 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” (i.e., do what's appropriate for that type of object) in response to the same method
call is the key concept of polymorphism. The same message (in this case, move ) sent to a
variety of objects has many forms of results—hence the term polymorphism.
 
 
Search WWH ::




Custom Search