O'Reilly logo

Sams Teach Yourself Object Oriented Programming in 21 Days, Second Edition by Anthony Sintes

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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() { ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required