Oracle Certified Associate Java Se 8 Programmer · Free Practice Question Hard
Question 50
Consider the following code:
- interface Interf
- {
- default void m1()
- {
- System.out.println("interface default method");
- }
- }
- public class Test implements Interf
- {
- default void m1()
- {
- System.out.println("Test class method");
- }
- public static void main(String[] args)
- {
- Interf i = new Test();
- i.m1();
- }
- }
What is the result ?
-
A
Test class method
-
B
Compilation Fails.
-
C
interface default method
-
D
An exception is thrown at runtime.
Reveal correct answer
Correct answer: B
Explanation
We can declare methods with default keyword only in interface but not in class. Hence we will get compile time error.Test.java:10: error: modifier default not allowed here
default void m1()
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
You must be logged in to post a comment.
