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

Answer:

  1. The programmer can explicitly write a no-argument constructor for a class.
  2. If the programmer does not write any constructors for a class, then a no-argument constructor (called the default constructor) is automatically supplied.
  3. If the programmer writes even one constructor for a class then the default constructor is not automatically supplied.

In the example program, the class definition for Video includes a constructor, so the default constructor was not automatically supplied. So the constructor proposed for Movie causes a syntax error. Let us not use it.


Instantiating Movie

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

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 have been added to Movie objects so the director and rating are not printed out.


QUESTION 14:

Why not change toString() in Video to this:

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