2

When I do this the code throws no error,

public class MyClass {    

    interface MyInnerInterface {}

    public Integer x = 0;

}

But when i try to add a method to the inner interface it throws error,

public class MyClass {    

    interface MyInnerInterface {public void fun();}

    public Integer x = 0;

}

How to declare an inner interface in a class with it's method signatures ? Also, are inner interface private when no access modifier specified ? I know they have to be public or global when declared at the top level.

1

Interfaces do not specify a method access level. Take out the word public. If this interface should be accessible outside the class, you do need to specify an access level greater than private.

public class MyClass {    

    // Can only be used in this class
    interface MyPrivateInnerInterface {
      void fun();
    }
    // Can be used in any class in the namespace
    public interface MyPublicInnerInterface {
      void fun();
    }
    // Can be used in any namespace!!!
    global interface MyGlobalInnerInterface {
      void fun();
    }

    public Integer x = 0;

}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.