Java OCA OCP Practice Question 1471

Question

Consider the following class:

public class MyBaseClass{ 
   public MyBaseClass (int port) throws IOException{ 
      //...lot of valid code. 
   } 
      //...other valid code. 
}

You want to write another class MyClass that extends from MyBaseClass.

Which of the following statements should hold true for MyClass class?

Select 1 option

  • A. It is not possible to define MyClass that does not throw IOException at instantiation.
  • B. MyBaseClass class itself is not valid because you cannot throw any exception from a constructor.
  • C. MyClass's constructor cannot throw any exception other than IOException.
  • D. MyClass's constructor cannot throw any exception other than subclass of IOException.
  • E. MyClass's constructor cannot throw any exception other than superclass of IOException. F. None of these.


Correct Option is  : F

Note

As MyBaseClass has only one constructor, there is only one way to instantiate it.

To instantiate any subclass of MyBaseClass, the subclass's constructor should call super(int).

But that throws throws IOException.

And so it must be defined in the throws clause of subclass's constructor.

Note that you cannot do something like:

public MyClass (){ 
   try{ super ();  }catch (Exception e){} //WRONG  : call to super must be firs 
} 

Constructor must declare all the checked exceptions declared in the base constructor.

They may add other exception.




PreviousNext

Related