Abstract
In this section, we'll take a quick look at an important idea that relates to inheritance in Java. To wrap our heads around what we're going to talk about, I think it's best if we start an existing project in the system. So let's take a look at the code we have in the code files.
So far, we've done the following:
- The
mainmethod of our program creates a list of objects. These objects are either of the typeBookorPoem, but we've placed them in a list ofLiteratureobjects, leading us to believe that theBookandPoemclasses must inherit from or extend theLiteratureclass. - Once we've built this array, we simply iterate through it using a
forloop and call thePrintmethod of thisforloop on each object. - At this point, we're dealing with objects as
Literatureobjects, not the Books or Poems that they are at the lowest level. This leads us to believe that theLiteratureclass itself must implement aPrintmethod; if we jump into the class, we'll see that this is true indeed.
However...