interface I{ void f1(); // 1 public void f2(); // 2 protected void f3(); // 3 private void f4(); // 4 } which lines generate compile time errors?
Reveal answer
Fill a bubble to check yourself
interface I{ void f1(); // 1 public void f2(); // 2 protected void f3(); // 3 private void f4(); // 4 } which lines generate compile time errors?
compiletime error at lines 1,2,3,4
compiletime error at line 3
compiletime error at line 1
compiletime error at lines 3,4
None of the above
To solve this question, the user needs to know about the different access modifiers in Java and their effect on the accessibility of methods.
Option A: This option is incorrect. There should be no compile-time errors in the code as all the methods in the interface are correctly defined.
Option B: This option is incorrect. The protected access modifier can be used in interface methods, so there should be no compile-time error at line 3.
Option C: This option is incorrect. The default access modifier can be used in interface methods, so there should be no compile-time error at line 1.
Option D: This option is correct. The private access modifier cannot be used in interface methods as they are implicitly abstract and intended to be implemented by other classes. Thus, there is a compile-time error at line 4. Additionally, the protected access modifier can be used in interface methods, but it may cause issues with implementation, so it is not recommended.
Option E: This option is incorrect. The correct answer is option D.
Therefore, the answer is: D. compiletime error at lines 3,4.