Consider the following code snippet:
// A.java public class A { private String name; protected A(String n) { name = n; } } // B.java public class B extends A { public B(String n) { super(n); } public String toString() { return name; } }
What's wrong with the above code? Select all that apply.
This code will encounter an access error at runtime. |
This code will not compile, because A's constructor is not declared public. |
This code will not compile, because class A is not declared protected. |
This code will not compile, because B's "toString" cannot access A's "name" field. |
Program: // A.java public class A { private String name; protected A(String n) { name = n; } } // B.java public class B extends A { public B(String n) { super(n); } public String toString() { return name; } }
What's wrong with the above code? Select all that apply.
Output:
Answer:
Option 2) This code will not compile, because A's constructor is not declared public.
Option 4) This code will not compile, because B's "toString" cannot access A's "name" field.
Get Answers For Free
Most questions answered within 1 hours.