Answer:
- The programmer can write a no-argument constructor for a class.
- If the programmer does not write any constructors for a class, then a no-argument constructor (called the default constructor) is automatically supplied.
- If the programmer writes even one constructor for a class then the default constructor is not automatically supplied.
Instantiating Movie
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.
Here is an example program 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" );
item1.show();
item2.show();
}
}
(You can run this program by using a text editor to copy and paste the class definitions from the previous pages into a source file.) The program prints:
Microcosmos, 90 min. available:true Jaws, 120 min. available:true
The statement item2.show() calls the show() method
of item2.
This method was inherited without change from the class
Video.
This is what it looks like:
public void show()
{
System.out.println( title + ", " + length + " min. available:" + avail );
}
It does not mention the new variables that have been added to objects
of type Movie, so nothing new is printed out.
QUESTION 14:
Why not change show() in Video
to include the line
System.out.println( "dir: " + director + rating );