All video tapes will have a rental price,
so the fixes should be made to the parent class VideoTape
.
If a new member varible rent
is added to the base class,
and if its constructor and its show() method are modified,
the two child classes will inherit these changes.
Fixing the parent class fixes all of its children.
Object
Remember that rule, that every constructor starts out by
invoking a super(...)
constructor
even if the call is not explicitly written.
Now look at the definition of VideoTape
:
class VideoTape { String title; // name of the item int length; // number of minutes boolean avail; // is the tape in the store? // constructor public VideoTape( String ttl, int lngth ) { title = ttl; length = lngth; avail = true; } public void show() { System.out.println( title + ", " + length + " min. available:" + avail ); } }
According to the rule the constructor implicitly does this:
// constructor
public VideoTape( String ttl, int lngth )
{
super(); // use the super class's constuctor
title = ttl; length = lngth; avail = true;
}
This is correct.
All classes have a parent class (a super class) except one.
The class at the top of the Java class hierarchy is called
Object
.
If a class definition does not specify a parent class then
it automatically has Object
as a parent class.
The compiler automatically assumes, for example:
class VideoTape extends Object
{
. . .
}