Question du test Java - Les bases

Laquelle des affirmations suivantes s'applique aux méthodes d'une interface en Java? (à partir de java 8)

Intermédiaire

Laquelle des affirmations suivantes s’applique aux méthodes d’une interface en Java? (a partir de java 8)

Auteur: NathalieStatut : Publiée(Mise à jour)Question passée 1105 fois
Modifier
2
Évaluations de la communauté
developer avatar
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."
developer avatar
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
developer avatar
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 !
developer avatar
Auteur anonyme
15/06/2024
Interface methods are "abstract" by default, and since java 8 we can implement methods, since java 9 we can use private and protected for methods
developer avatar
Réponse incorrecte
Raymond
20/12/2023
By reading Ian's comment, I agree with him and chose the same answer. PS: I am too stingy to spend one correction point to see the solution :)