Exercises in this lecture        next -- Keyboard shortcut: 'n'  Go to the slide, where this exercise belongs -- Keyboard shortcut: 'u'  

Exercise 7.3
Private Visibility and inheritance


Take a look at the classes shown below:

using System;

public class A{
  private int i = 7;

  protected int F(int j){
   return i + j;
  }
}

public class B : A{
  public void G(){
    Console.WriteLine("i: {0}", i);
    Console.WriteLine("F(5): {0}", F(5));
  }
}

public class Client {
  public static void Main(){
    B b = new B();
    b.G();
  }
}

Answer the following questions before you run the program:

  1. Does the instance of B, created in Main in Client, have an instance variable i?
  2. Is the first call to Console.WriteLine in G legal?
  3. Is the second call to Console.WriteLine in G legal?

Run the program and confirm your answers.


Solution