Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

11. Analyze and the select the correct answer/s: public class A extends B{ class

ID: 3714200 • Letter: 1

Question

11. Analyze and the select the correct answer/s: public class A extends B{ class B public B(String s) s A. The program has a compilation error because A does not have a default constructor. B. The program has a compilation error because the default constructor of A invokes the default constructor of B, but B does not have a default constructor. C. The program would compile fine if you add the following constructor into A: A(String s) D. The program would compile fine if you add the following constructor into A: A(String s) { AlString s) ) super(s);

Explanation / Answer

11.) Option B and D is correct. This is regarding Inheritence. Here Base Class Is B and Class A extends B.

class B has a parameterized Constructor. So as class A extends B so to compile the program properly B must have default constructor as well because if you don't define the Default Constructor then while creating object of A it will first call the default constructor of B which is not present. So compile forces you to define default constructor in Class B along with parametrized constructor.

Option D is also Correct. Because the class A has to call the constructor of class B if not default then parametrized constructor. So if you define one constructor in class A which calls its base class constructor at the very begining then its fine.

So if you add constructor

A(String s){super(s)}; compiler will not give any compilation error.

Option A is wrong because it doesn't matter A has default constructor or not only thing which concern is we have to initiialize the Class B object first whether By calling default constructor of B or Parametrized Construtor of B. Declaring Default Constructor of A will not full fil this purpose.

Option C is also incorret because you are just adding a parametrized constructor of A but not calling constructor of B so Class B object is not initialized and compiler will give compilation error.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote