Which two methods, inserted individually, correctly complete the Three class? (Choose two.)

Given:

1. class One {
2. public One foo() {
3. return this;
4. }
5. }
6.
7. class Two extends One {
8. public One foo() {
9. return this;
10. }
11. }
12.
13. class Three extends Two {
14. // insert method here
15. }

Which two methods, inserted individually, correctly complete the Three class? (Choose two.)

Given:

1. class One {
2. public One foo() {
3. return this;
4. }
5. }
6.
7. class Two extends One {
8. public One foo() {
9. return this;
10. }
11. }
12.
13. class Three extends Two {
14. // insert method here
15. }

Which two methods, inserted individually, correctly complete the Three class? (Choose two.)

A.
public void foo() {}

B.
public int foo() { return 3; }

C.
public Two foo() { return this; }

D.
public One foo() { return this; }

E.
public Object foo() { return this; }

Explanation:
A:
Main.java:14: foo() in Three cannot override foo() in Two; attempting to use incompatible return type
found : void
required: One
public void foo() {}
^
1 error

B:
Main.java:14: foo() in Three cannot override foo() in Two; attempting to use incompatible return type
found : int
required: One
public int foo() { return 3; }
^
1 error

C:
compiled successfully

D:
compiled successfully

E:
Main.java:14: foo() in Three cannot override foo() in Two; attempting to use incompatible return type
found : java.lang.Object
required: One
public Object foo() { return this; }
^
1 error



Leave a Reply 1

Your email address will not be published. Required fields are marked *