Sobes.tech
Junior — Middle

How do access modifiers differ from each other in Java and Kotlin languages?

sobes.tech AI

Answer from AI

In Java and Kotlin, access modifiers have similar goals but differ in set and behavior:

  • Java:

    • public — accessible from anywhere.
    • protected — accessible within the package and in subclasses.
    • (package-private) — accessible only within the package (if no modifier is specified).
    • private — accessible only within the class.
  • Kotlin:

    • public (default) — accessible from anywhere.
    • internal — accessible within the module (e.g., within a single Gradle module).
    • protected — accessible in subclasses, but only within the same file (unlike Java, where protected allows access from the package).
    • private — accessible within the class or file (if declared at the file level).

The main difference is that Kotlin has the internal modifier, which restricts visibility to the module, and protected in Kotlin does not provide access within the package, only in subclasses. In Java, there is no internal, and protected provides access both within the package and in subclasses.

How do access modifiers differ from each other in… - sobes.tech