go to previous page   go to home page   go to next page hear noise highlighting

Answer:

If a parent and a child both have a feature (like a title), then the feature should be put in the parent and inherited by the child.


Instantiating Movie

Here is a version of main() that makes use of the two classes (original versions):

public class VideoStore
{
  public static void main ( String args[] )
  {
    Video item1 = new Video("Microcosmos", 90 );
    Movie item2 = new Movie("Jaws", 120, "Spielberg", "PG" );
    System.out.println( item1.toString() );
    System.out.println( item2.toString() );
  }
}

The program prints:

Microcosmos, 90 min. available:true
Jaws, 120 min. available:true

The statement item2.toString() calls the toString() method of item2. This method was inherited without change from the class Video. This is what it looks like:

public String toString()
{
  title + ", " + length + " min. available:" + avail ;
}

It does not use the new variables that Movie objects have, so the director and rating are not printed out.

It would be worthwhile (of course) to download a play with the complete program. Here is the link: Video Store.


QUESTION 15:

Why not change toString() in Video to this:

public String toString()
{
  title + ", " + length + " min. available:" + avail +  
  "dir: " + director + ", rating: " + rating );
}
 

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