Chapter 7. Polymorphism: Time to Write Some Code

Yesterday you learned about polymorphism. You should have a good understanding of the four different types of polymorphism. Today you will gain hands-on experience with polymorphism through a number of lab exercises. By the end of today’s lesson, you should feel a bit more comfortable with the theory presented in Day 6, “Polymorphism: Learning to Predict the Future.”

Today you will learn

  • How to apply the different forms of polymorphism

  • How to write future proof software

  • How polymorphism can help you avoid switch logic

Lab 1: Applying Polymorphism

In Day 5, Lab 2 introduced you to an employee hierarchy. Listing 7.1 presents a slightly altered Employee base class.

Example 7.1. Employee.java

public abstract ...

Get Sams Teach Yourself Object Oriented Programming in 21 Days, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.