In: Computer Science
How can the following code be corrected? Give at least two good answers.
1 public class H2ClassC {
2 H2ClassC (int a) {}
3 } // end class H2ClassC
4
5 class H2ClassD extends H2ClassC{
6 } // end class H2ClassD
=> in this case, H2ClassD class contains a default constructuor, While the base class defines explicit parameterized constructor, Due to which a compilation error will occur. First Solution => To correct this, H2ClassC needs to Define a default constructor. public class H2ClassC { H2ClassC (int a) {} H2ClassC () {this(0);} } // end class H2ClassC class H2ClassD extends H2ClassC{ } // end class H2ClassD Second Solution => To correct this, H2ClassD needs to explicitly call the base class constructor. public class H2ClassC { H2ClassC (int a) {} } // end class H2ClassC class H2ClassD extends H2ClassC{ H2ClassD () {super(0);} } // end class H2ClassD
************************************************** Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.