Java OCA OCP Practice Question 60

Question

Consider the following class definition:

1. public class Test extends Base { 
2.   public Test(int j) { 
3.   } 
4.   public Test(int j, int k) { 
5.     super(j, k); 
6.   } 
7. } 

Which of the following forms of constructor must exist explicitly in the definition of the Base class?

Assume Test and Base are in the same package.

Choose all that apply.

  • A. Base() { }
  • B. Base(int j) { }
  • C. Base(int j, int k) { }
  • D. Base(int j, int k, int l) { }


A, C.

Note

The constructor at lines 2 and 3 includes no explicit call to either this() or super(), which means that the compiler will generate a call to the no-args superclass constructor, as in A.

The explicit call to super() at line 5 requires that the Base class must have a constructor as in C.

This requirement has two consequences.

First, C must be one of the required constructors and therefore one of the answers.

Second, the Base class must have at least that constructor defined explicitly, so the default constructor is not generated but must be added explicitly.

Therefore the constructor of A is also required and must be a correct answer.

At no point in the Test class is there a call to either a superclass constructor with one or three arguments, so B and D need not explicitly exist.




PreviousNext

Related