Search code examples
javajava-8abstract-classdefaultaccess-specifier

Why can I define only default and static methods inside a java interface?


Why can I define only default and static methods inside a java interface,while other access modifiers like protected and public having much more privilege than default can't be used?

interface int1
{

     default void add(int a, int b)
    {

    }
    static void sub(int a, int b)
    {

    }

}
interface int1
{

    public void add(int a, int b)
    {

    }
    protected void sub(int a, int b)
    {

    }

}

--shows error message at compile time "Abstract methods do not specify a body"


Solution

  • The reason we have default methods in interfaces is to allow the developers to add new methods to the interfaces without affecting the classes that implements these interfaces. Here is link for complete article.