Sobes.tech
Junior — Middle

Is it possible to declare an interface with a non-public access level?

sobes.tech AI

Answer from AI

In Java, interfaces by default have public or package (default) access level if no modifier is explicitly specified.

  • If an interface is declared outside a class (at the package level), it can be either public or have package access (without a modifier). Other access levels, such as private or protected, are not allowed for top-level interfaces.

  • Inside a class (nested interface), an interface can be declared with any access level, including private and protected.

Example:

// Package access
interface MyInterface {}

// Public interface
public interface MyPublicInterface {}

class Outer {
    private interface InnerInterface {}
}