Knowee
Questions
Features
Study Tools

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

Question

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

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

Solution

The code will be compiled and executed successfully. The output will be "inside class". This is because the class "WithDefinitionsImpl" overrides the default method "definedMeth()" provided by the interface "WithDefinitionsInter". So, when the method is called using the reference of the interface, the overridden method in the class will be executed.

Similar Questions

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

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

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

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.