Exercise - Inheritance 1
1.
Here is a program that uses a
class Video to represent videos available at a rental store. Try to trace out
what will this program print.
class Video
{
private
String title; // name of the item
private
int length; // number of minutes
private
boolean avail; // is the video in the
store?
//
constructor
public Video( String ttl )
{
title = ttl; length = 90; avail = true;
}
//
constructor
public Video( String ttl, int lngth )
{
title = ttl; length = lngth; avail = true;
}
public void show()
{
System.out.println(title + ", " + length + " min.
available:" + avail );
}
}
public class VideoStore
{
public static void main ( String args[] )
{
Video item1 = new Video("Jaws", 120 );
Video item2 = new Video("Star Wars" );
item1.show();
item2.show();
}
}
2. Output:
Jaws, 120 min. available:true
Star Wars, 90 min. available:true
3.
Let’s make a Movie class that is
similar to Video, but also includes the name of the director and a rating. The class
is given as follows:
class Movie extends Video
{
private
String director; // name of the director
private String rating; // U, SG, SX
//
constructor
public Movie( String ttl, int lngth, String dir, String rtng )
{
super( ttl, lngth ); //use
the super class's constructor
director = dir; rating = rtng;
}
}
4.
The class Movie is a
subclass of Video. List all of the members of a Movie object and for
each member state whether it is inherited from Video or defined in Movie.
title - defined in Video
length - defined in Video
avail - defined in Video
director - defined in Movie
rating - defined in Movie
5.
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();
}
}
What does this print? Can you explain the output?
Output:
Microcosmos, 90 min. available:true
Jaws, 120 min. available:true
item 1 is defined and printed as a video with title, length and avail while item 2 is also defined and printed as movie with title, length and avail.
6.
To print out all data members
of a Movie object, we need to override
the show() method that is inherited from
Video. Change the definition of
the Movie class to override the parent’s show() method:
@Override
public void show()
{
super.show();
System.out.println(director + ", " + rating );
}
7.
Now compile and run again the
VideoStore program given in step 5. This
time what does the program print?
Microcosmos, 90 min. available:true
Jaws, 120 min. available:true
Spielberg, PG
8.
Overload the Movie constructor by
adding the following constructor definition in the Movie class
and recompile the class (Make sure there is no error).
public Movie( String ttl, String dir, String
rtng )
{
super( ttl ); // invoke the matching
parent class constructor
director = dir; rating =
rtng; // initialize members unique to
Movie
}
Does
a child constructor always invoke a parent constructor?
Try removing the
statement:
super(
ttl );
in
the above constructor. Now compile the class and you’ll get a compilation
error. Explain why? Then make the correction for the error.
Answer: not necessarily. it only invokes when the super method is used. The error happens because the parent class constructor was not invoked.
Comments
Post a Comment