자격증/OCJP_ExamC

QUESTION 14

IT grow. 2018. 7. 21. 17:31
반응형
QUESTION 14
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; }
Solution : CD



plus imformation 


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 


 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 


반응형