Yes. When a string is needed toString()
is automatically called.
super
in a Child's Method
The toString()
method in the child class Movie
is nearly the same as the one in Video
.
Often (as in the example) you want a method in a child class that
adds features to a method in a parent class.
You can use the super
reference in this situation.
For example,
here is Video's
method:
public String toString() { return getTitle() + ", " + getLength() + " min. " ; }
Here is Movie's
method without using super:
public String toString() { return getTitle() + ", " + getLength() + " min. available:" + getAvailable() + " dir: " + director + ", rating: " + rating ; }
Movie
's method would better be written using super:
public String toString()
{
return super.toString() + " dir: " + director + ", rating: " + rating ;
}
Unlike the case when super
is used in a constructor,
when super
is used in a method it does not have to be the first statement.
Think of two reasons why using super
in this way is
a good thing to do.