Quiz 2 (Exercises from Chap. 11)

Figure 1 is meant to show that class A is abstract and that classes B and C are concrete subclasses. In class A, only method m1 is abstract, the other two methods are implemented. Both B and C implement method m1. B overrides m2, and C overrides m3.

Figure 3 shows that Y is a subclass of X. Instance variables of X are x1 (an int) and name (a String), which you may assume are private. Y has an extra instance variable called y1 (an int).

  1. Circle the illegal statements.
  2. Write an equals method for X which indicates that two X objects are equal if they have the same x1 and name and neither reference is null.
  3. Write an equals method for Y which indicates that two Y objects are equal if they have the same x1 and name and y1 and neither reference is null.
  4. Write a toString method for X which gives a form like
    X[x1 = 5, name = xenon]
  5. Write a toString method for Y which gives a form like
    Y[x1 = 5, name = xenon][y1 = 10]