Medium
Which of the following statements applies to methods of an interface in Java? (from java 8)
Edit
2
Community Evaluations
Ian
22/05/2023
I answered .
The statement "An interface can only contain abstract methods" is true for interfaces in Java.
An interface in Java can only declare abstract methods, which means they don't have an implementation. Any class that implements the interface must provide an implementation for these methods.
The statement "You can define a method in an interface" is false. In an interface, you can only declare method signatures (abstract methods) without providing an implementation.
The statement "The private and protected access modifiers can also be used to declare methods in an interface" is false. In Java, methods in an interface are implicitly public. It is not possible to define private or protected methods within an interface.
Therefore, the correct answer is: "An interface can only contain abstract methods."
Auteur anonyme
03/06/2023
Well actually, you can define declare method implementation in interface from Java 8 on thanks to the "default" keyword, so the test is correct
Auteur anonyme
05/06/2023
As Kendall answered you can declare default method from java 8. I will update the question to add then mention of java 8. Thank you both for your comments !