Knowee
Questions
Features
Study Tools

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

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::println

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

Solution

The code will not compile successfully. The reason is that the method implementedMethod() in the class Quiz does not correctly override the default method from the DefaultMethodInterface interface.

In Java, when a class implements an interface, it needs to either:

  1. Declare the method as public because all methods in an interface are implicitly public. The visibility can't be reduced in the implementing class.

  2. Use the @Override annotation if it's intending to override a method from an interface or superclass.

So, the correct code should be:

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

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

Now, when the implementedMethod() is called on an instance of 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::printlnSystem::out::println()System.out::println()SubmitPowered by Infosys Win

@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

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.