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

Answer:

  1. You should not have to write the same code more than once.
  2. A change made to the method in the parent class is inherited by the child class.

Problems with Private

Look at the instance variables in Video:

class Video
{
  private String  title;    // name of the item
  private int     length;   // number of minutes
  private boolean avail;    // is the video in the store?

  . . .

They are declared private, which means that only code that is part of Video can see these variables. Even though they are included by inheritance in the child class Movie, they cannot directly be used in that class. This code does not compile:

class Movie extends Video
{
 . . .
  public String toString()
  {
    return title + ", " + length + " min. available:" + avail +  // these private variables can't be accessed
           " dir: " + director + ", rating:  " + rating ; 
  }
  . . .
}

It tries to use private variables of the parent class.

One way to gain access to private variables is to use getter and setter methods of the parent class (as was done a few pages ago.)


QUESTION 18:

Why not make all the variables public ?


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