go to previous page   go to home page   go to next page

Answer:

Jaws, 120 min. available: true
Star Wars, 90 min. available: true

Using Inheritance

The Video class has basic information in it, and could be used for documentaries and instructional videos. But more information is needed for movie videos. Let us make a class that is similar to Video, but now includes the name of the director and a rating.

class Video
{
  String  title;    // name of the item
  int     length;   // number of minutes
  boolean avail;    // is the video in the store?

  ...               // as above
}

class Movie extends Video
{
  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 base class's constructor to initialize members inherited from it
    director = dir;           // initialize what's new to Movie
    rating = rtng;      
  }

}  



Movie as a child of Video

The class Movie is a subclass of Video. An object of type Movie has these members:

member  
title inherited from Video
length inherited from Video
avail inherited from Video
toString() inherited from Video
directordefined in Movie
rating defined in Movie

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.



QUESTION 10:

Does a child class inherit both variables and methods?