Knowee
Questions
Features
Study Tools

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

Question

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

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

Solution

The code will not get compiled because the method definedMeth() is undefined in Object class.

In the class WithDefinitionsImpl, the method definedMeth() is trying to call the super method, which would refer to the parent class. However, since this class is implementing an interface and not extending a class, there is no super method to call.

Interfaces in Java can have default methods (like definedMeth() in this case), but they cannot be called using the super keyword in the implementing class. This is because interfaces do not form part of the class hierarchy, so there is no concept of a 'super' interface.

Therefore, the compiler will throw an error at the line "super.definedMeth();" in the WithDefinitionsImpl class.

This problem has been solved

Similar Questions

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

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

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

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.