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
main
method of our program creates a list of objects. These objects are either of the typeBook
orPoem
, but we've placed them in a list ofLiterature
objects, leading us to believe that theBook
andPoem
classes must inherit from or extend theLiterature
class. - Once we've built this array, we simply iterate through it using a
for
loop and call thePrint
method of thisfor
loop on each object. - At this point, we're dealing with objects as
Literature
objects, not the Books or Poems that they are at the lowest level. This leads us to believe that theLiterature
class itself must implement aPrint
method; if we jump into the class, we'll see that this is true indeed.
However...