Knowee
Questions
Features
Study Tools

@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

Question

@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

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

The compilation of the above code will not be successful. This is because the method implementedMethod() in the class Quiz is not properly overriding the default method from the interface DefaultMethodInterface.

In Java, when a class implements an interface, it needs to override the methods of the interface. However, in this case, the method in the class Quiz does not have the @Override annotation, and it also does not have the same access modifier as the method in the interface. The method in the interface is default, while the method in the class is package-private (no access modifier).

To fix this, you can add the @Override annotation above the method in the class, and change the access modifier of the method in the class to public. Here is the corrected code:

@FunctionalInterface
interface DefaultMethodInterface {
    default void implementedMethod() {
        System.out.println("inside interface");
    }
}

public class Quiz implements DefaultMethodInterface {
    @Override
    public void implementedMethod() {
        System.out.println("inside class");
    }
}

With these changes, the code will compile and run successfully. When the implementedMethod() is called on an object of the class Quiz, it will print "inside class".

This problem has been solved

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

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

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

Select the correct answerWhat is the output of the following program?interface OuterInterface{  public void InnerMethod();  public interface InnerInterface  {    public void InnerMethod();  }}public class Outer implements OuterInterface.InnerInterface, OuterInterface{  public void InnerMethod()  {    System.out.println(100);  }          public static void main(String[] args)  {    Outer obj = new Outer();    obj.InnerMethod();  }}Options100Compilation ErrorRuntime ErrorNone of the above

1/3

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.