Making the variables of the parent public
would solve this problem,
but would violate modularity.
If a variable or method in a class is made protected
,
then subclasses and other classes in the same package can access it.
For now, regard classes in the same package as all being in the same disk directory (folder).
Declaring a member protected
opens it up to much more access than
private
but not as much as public
.
Here is how Video
and Movie
can be changed:
class Video
{
protected String title; // name of the item
protected int length; // number of minutes
protected boolean avail; // is the video in the store?
. . .
}
class Movie extends Video
{
. . .
public String toString()
{
return title + ", " + length + " min. available:" + avail + // these protected variables CAN NOW be accessed
" dir: " + director + ", rating: " + rating ;
}
. . .
}
(Review: ) Is it possible for a parent class to have several children classes?