public String toString() { return title + length + " min. available:" + avail + "\nartist:" + artist + " style: " + category ; }
Yes, this will work. Some of the variables are declared in Video
, but they are now
declared to be protected
and so can be accessed.
You may have noticed a major design flaw in the definitions of these classes — none of the videos have a price! Say that it is your job to fix this problem. What changes will you make?
All videos have a price, so make changes to the parent class Video
.
The two child classes will inherit the changes.
A new variable price
can be added to the parent class.
Modify its constructor and its toString()
method.
Remember the rule: every constructor starts out
with a super()
constructor.
If you don't explicitly put it in,
then the Java compiler automatically puts it in for you.
Now look at the definition of Video
.
The constructor has been modified for the new variable.
class Video { private String title; // name of the item private int length; // number of minutes private boolean avail; // is the video in the store? private double price; // price, in cents // constructor public Video( String ttl, int lngth, int pr ) { title = ttl; length = lngth; avail = true; price = pr; } public String toString() { return title + ", " + length + " min. available:" + avail " price: $" + price ; } // setters and getters . . . }
The constructors in the child classes need to be modified to match the new constructor. Also, some new setters and getters have been added.
The complete program including recent changes is available here: Video Store. Save it to a file.
What is the parent class of Video
?