Exercises in this lecture   Go to the notes, in which this exercise belongs -- Keyboard shortcut: 'u'   Alphabetic index   Course home   

Exercise solution:
Non-virtual variables - Virtual Methods


The program prints

  1
  2
  2
  1

The first output line reflects that instance variables are non-virtual. The static type of the variable a controls the result.

The second output line reflects that the method Op1 is virtual. Op1 in class B accesses v in B.

The third output line shows, like the first one, that instance variables are non-virtual.

The fourth output line shows that the variable v from A is present in an instance of class B.