Chapter 5. Inheritance: Time to Write Some Code

Inheritance is a powerful tool. Today you will explore the use of this new tool through a number of hands-on lab exercises. By the end of today’s lesson, you should feel a bit more comfortable with the theory presented on Day 4.

Today you will learn

  • How to use inheritance while programming

  • How abstract classes help you plan inheritance

  • About the importance of the “Is-A” and “Has-A”

  • How Java may have violated the “Is-A” and “Has-A” relationship

Lab 1: Simple Inheritance

Listing 5.1 presents the personified MoodyObject baseclass.

Example 5.1. MoodyObject.java

public class MoodyObject { // return the mood protected String getMood() { return "moody"; } // ask the object how it feels public void queryMood() { ...

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.