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
publicor have package access (without a modifier). Other access levels, such asprivateorprotected, are not allowed for top-level interfaces. -
Inside a class (nested interface), an interface can be declared with any access level, including
privateandprotected.
Example:
// Package access
interface MyInterface {}
// Public interface
public interface MyPublicInterface {}
class Outer {
private interface InnerInterface {}
}