Question

In: Computer Science

How can the following code be corrected? Give at least two good answers. 1 public class...

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

Solutions

Expert Solution

=> 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.


Related Solutions

How can a privately produced good be a public good? Give examples. How can a government...
How can a privately produced good be a public good? Give examples. How can a government produced good be a private good?
1. Convert the following code shown below to C++ code: public class HighwayBillboard { public int...
1. Convert the following code shown below to C++ code: public class HighwayBillboard { public int maxRevenue(int[] billboard, int[] revenue, int distance, int milesRes) { int[] MR = new int[distance + 1]; //Next billboard which can be used will start from index 0 in billboard[] int nextBillBoard = 0; //example if milesRes = 5 miles then any 2 bill boards has to be more than //5 miles away so actually we can put at 6th mile so we can add...
What is wrong with this code and how can it be fixed? import java.util.Scanner; public class...
What is wrong with this code and how can it be fixed? import java.util.Scanner; public class admissionRequirement { public static void main(String[] args) { // TODO Auto-generated method stub Scanner myObj = new Scanner(System.in); System.out.println("What is your name?"); String name = myObj.nextLine(); System.out.println("What is your Reading Score?"); int reading = myObj.nextInt(); System.out.println("What is your Math Score?"); int math = myObj.nextInt(); System.out.println("What is your Writing Score?"); int writing = myObj.nextInt(); System.out.println("What is your Class Standing?"); int standing = myObj.nextInt(); System.out.println("What is...
1.can only government supply a public good? 2. what is a public good 3. How is...
1.can only government supply a public good? 2. what is a public good 3. How is a Public Good Different than a Private Good 4. What are the Challenges related to public good? How should the “ Free Rider” problems should be addressed.
What output is produced by the following code?   Explain how it works. public class A {...
What output is produced by the following code?   Explain how it works. public class A { int a = 1; int b = 2; public int getSum(int a, int b) {     this.a+=a;     this.b+=b;     return this.a + this.b; } } public class B extends A { int a = 3; int b = 4; public int getSum(int a, int b) {     this.b=a;     super.b=b+b;     return super.a+this.b; } } public class q2 { public static void main(String[]...
Discuss how public good are used for the betterment of the general public. Provide at least...
Discuss how public good are used for the betterment of the general public. Provide at least three examples and provide details on the general government’s expenditure on those goods.
Is a college class at a public university a Private good, Public good, or Club good
Is a college class at a public university a Private good, Public good, or Club good
Can you fix the errors in this code? import java.util.Scanner; public class Errors6 {    public...
Can you fix the errors in this code? import java.util.Scanner; public class Errors6 {    public static void main(String[] args) {        System.out.println("This program will ask the user for three sets of two numbers and will calculate the average of each set.");        Scanner input = new Scanner(System.in);        int n1, n2;        System.out.print("Please enter the first number: ");        n1 = input.nextInt();        System.out.print("Please enter the second number: ");        n2 =...
GIVEN THE CODE BELOW Given the following class: /** * Document class. */ public class Document...
GIVEN THE CODE BELOW Given the following class: /** * Document class. */ public class Document { private int words; /** * constructor * pre: none * post: A Document object created. Words initialized to 0. */ public Document() { words = 0; //default words } /** * Changes the number of document words. * pre: none * post: Words has been changed. */ public void setWords(int numWords) { words = numWords; } /** * Calculates the number of pages....
identify the syntax errors in the following code:             public class Hello {                    &
identify the syntax errors in the following code:             public class Hello {                         private static int main(String [] args) {                                     string Msg=”Hello, Wrld!;                                     Sytem.out.println(msg+ “Ken")
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT