Java OCA OCP Practice Question 1562

Question

Consider the following code:

public class SubClass extends SuperClass{
     int i, j, k;
     public SubClass ( int m, int n )      {  i = m ;  j = m ;   } //1
     public SubClass ( int m )   {   super (m );    } //2
}

Which of the following constructors must exist in SuperClass for SubClass to compile correctly?

Select 2 options

  • A. It is ok even if no explicit constructor is defined in SuperClass
  • B. public SuperClass (int a, int b)
  • C. public SuperClass (int a)
  • D. public SuperClass ()
  • E. only public SuperClass (int a) is required.


Correct Options are  : C D

Note

For A.

The //2 will fail as it needs a constructor taking an int!

For B.

It is not used anywhere so it is not necessary.

For C.

Because it is called in the second constructor of SubClass.

For D.

The default no args constructor will not be provided because SuperClass has to define one arg constructor.

For E.

You'll have to explicitly define a no args constructor because it is needed in the first constructor of SubClass.




PreviousNext

Related