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.

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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.