Jaws, 120 min. available: true Star Wars, 90 min. available: true
The VideoTape
class has basic information in it,
and would be OK for documentaries and instructional tapes.
But movies need more information.
Let us make a class that is similar to VideoTape,
but has the name of the director and a rating.
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 ) { title = ttl; length = 90; avail = true; } // constructor public VideoTape( String ttl, int lngth ) { title = ttl; length = lngth; avail = true; } public void show() { System.out.println( title + ", " + length + " min. available:" + avail ); } } class Movie extends VideoTape { String director; // name of the director String rating; // G, PG, R, or X // constructor public Movie( String ttl, int lngth, String dir, String rtng ) { super( ttl, lngth ); // use the super class's constructor director = dir; rating = rtng; // initialize what's new to Movie } }
The class Movie
is a subclass of VideoTape
.
An object of type Movie
has the following members in it:
member title inherited from VideoTape length inherited from VideoTape avail inherited from VideoTape show() inherited from VideoTape director defined in Movie rating defined in Movie
Both classes are defined: the VideoTape
class can be used
to construct objects of that type, and now the Movie
class
can be used to construct objects of the Movie
type.