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

4. Analyze the following code: public class Test extends A { public static void

ID: 3714193 • Letter: 4

Question

4. Analyze the following code: public class Test extends A { public static void main(Stringll args){ Test t- new Test0; t.printO; class A { String s; A(String s)i this.s-s; public void print0 i System.out.printn(s); a. The program does not compile because Test does not have a default constructor Test0 b. The program has an implicit default constructor Test0, but it cannot be compiled, because its super class does not have a default constructor. The program would compile if the constructor in the class A were removed. c. The program would compile if a default constructor A0 is added to class A explicitly. d. The program compiles, but it has a runtime error due to the conflict on the method name print.

Explanation / Answer

Answer)

b and c.

If a class has does not have constructor, then jvm will automatically(implicitly) provide a default constructor, here Test() is a default constructor provided by jvm but class Test is extending class A which has parametrized constructor but no default constructor, so class Test would produce compile time error.

To avoid compile time error,

b)parameterized constructor A(String s) should be removed, or

c) we need to add default constructor A() {} to class A explicitly.

After changes:

public class Test extends A

{

public static void main(String[] args)

{

Test t = new Test();

t.print();

}

}

class A

{

String s;

A() {} //default constructor

A(String s)

{

this.s=s;

}

public void print()

{

System.out.println(s);

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote