Look at the instance variables in Video
:
class Video { private String title; // name of the item private int length; // number of minutes private boolean avail; // is the video in the store? . . .
They are declared private
,
which means that only code that is part of Video
can see these variables.
Even though they are included by inheritance in the child class Movie
,
they cannot directly be used in that class.
This code does not compile:
class Movie extends Video
{
. . .
public String toString()
{
return title + ", " + length + " min. available:" + avail + // these private variables can't be accessed
" dir: " + director + ", rating: " + rating ;
}
. . .
}
It tries to use private
variables of the parent class.
One way to gain access to private
variables is to use getter and setter methods
of the parent class (as was done a few pages ago.)
Why not make all the variables public
?