The Video
class has basic information,
and could be used for documentaries and instructional videos.
But more information is needed for movie videos.
Here is a class that is similar to Video
,
but now includes the name of the director and a rating.
class Video { private String title; // name of the item private int length; // number of minutes private boolean avail; // is the video in the store? ... // as above } class Movie extends Video { private String director; // name of the director private String rating; // G, PG, R, or X // constructor public Movie( String ttl, int lngth, String dir, String rtng ) { super( ttl, lngth ); // use the base class's constructor to initialize members inherited from it director = dir; // initialize what's new to Movie rating = rtng; } public String getDirector() { return director; } public String getRating() { return rating; } }
The class Movie
is a subclass of Video
.
An object of type Movie
has these members:
member | member | member | |||
---|---|---|---|---|---|
title | inherited from Video | getTitle() | inherited from Video | setTitle() | inherited from Video |
length | inherited from Video | getLength() | inherited from Video | setLength() | inherited from Video |
avail | inherited from Video | getAvailable() | inherited from Video | setAvailable() | inherited from Video |
director | defined in Movie | getDirector() | defined in Movie | ||
rating | defined in Movie | getRating() | defined in Movie | ||
toString() | inherited from Video |
Both classes are defined: the Video
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.
Objects of class Movie
have the members defined in Video
and the members defined in Movie
.
Does a child class inherit both variables and methods?