interface DefaultMethodInterface { default void implementedMethod() { System.out.println("inside interface"); }}public class Quiz implements DefaultMethodInterface { void implementedMethod() { System.out.println("inside class"); }}What will happen to the above code when compiled? System::out::printlnSystem.out::printlnSystem::out::println()System.out::println()SubmitPowered by Infosys Win
Question
interface DefaultMethodInterface { default void implementedMethod() { System.out.println("inside interface"); }}public class Quiz implements DefaultMethodInterface { void implementedMethod() { System.out.println("inside class"); }}What will happen to the above code when compiled? System::out::printlnSystem.out::printlnSystem::out::println()System.out::println()SubmitPowered by Infosys Win
Solution
The code will not compile successfully. The reason is that the method implementedMethod() in the class Quiz does not override the default method in the DefaultMethodInterface interface. In Java, when a class implements an interface, it needs to either declare the method as abstract or provide the method's implementation. However, the method signature in the class should exactly match with the method signature in the interface.
In this case, the method implementedMethod() in the Quiz
Similar Questions
interface DefaultMethodInterface { default void implementedMethod() { System.out.println("inside interface"); }}public class Quiz implements DefaultMethodInterface { void implementedMethod() { System.out.println("inside class"); }}What will happen to the above code when compiled? System::out::printlnSystem.out::println
@FunctionalInterfaceinterface DefaultMethodInterface { default void implementedMethod() { System.out.println("inside interface"); }}public class Quiz implements DefaultMethodInterface { void implementedMethod() { System.out.println("inside class"); }}What will happen to the above code when compiled? Compilation will be successful as it is a valid Functional InterfaceCompilation will not be successful as it is not a valid Functional InterfaceDefault keyword and @FunctionalInterface annotation should not be used together. So, the code will get compiled successfullyExecution will be successful as expected
What is expected when the following code gets compiled and executed?interface WithDefinitionsInter { default void definedMeth() { System.out.println("inside interface"); }}class WithDefinitionsImpl implements WithDefinitionsInter { public void definedMeth() { System.out.println("inside class"); }}public class QuizDef { public static void main(String par[]) { WithDefinitionsInter withDef = new WithDefinitionsImpl(); withDef.definedMeth(); }} No successful compilation because the interface does not even have a single abstract methodThe code will print, inside interface and inside class as a result of successful executionNo successful compilation because the interface holds method definitionThe code will be executed successfully. And, the execution result will be, inside class
interface WithDefinitionsInter { default void definedMeth() { System.out.println("inside interface"); }}class WithDefinitionsImpl implements WithDefinitionsInter { public void definedMeth() { super.definedMeth(); System.out.println("inside class"); }}public class QuizDef { public static void main(String par[]) { WithDefinitionsInter withDef = new WithDefinitionsImpl(); withDef.definedMeth(); }}What will happen to the above code when compiled? The code will not get compiled because the interface does not even have a single abstract methodThe code will not get compiled because the method definedMeth() is undefined in Object classThe code will not get compiled because the interface holds method definitionThe code will get compiled and executed successfully
public interface Deduction { static void deduct() { System.out.println("deduct"); }}public class Customer implements Deduction { public static void deduct() { System.out.println("deduction for customer"); }}public class TechSol { public static void main(String[] args) { Deduction deduction = new Customer(); deduction.deduct(); }}What will happen when the code is subjected to compilation and execution? The code will not get compiled as the static method of interface is not accessed using interface nameWill get executed successfully leaving the output “deduct”Will get executed successfully leaving the output “deduction for customer”Will lead to run time ambiguity as the interface’s static method is overridden
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.