Tarun Deep Attri
Tarun Deep Attri

Reputation: 8674

Modifier `protected` is not applicable inside `file` error in Kotlin

I have a class whose declaration is like :

class NetworkManagerImpl : NetworkManager { }

I wanted to make the class protected so that it is visible inside package only. But when i add protected in front of class like :

protected class NetworkManagerImpl : NetworkManager { }

It gives error as Modifier protected is not applicable inside file How to fix this error or more importantly how to make an entire class(Top level) protected?

Upvotes: 5

Views: 2793

Answers (1)

s1m0nw1
s1m0nw1

Reputation: 81899

As written in the docs, there is no protected modifier for top-level entities like classes.

Here’s a statement coming from a Kotlin team member:

The motivation for not having package protected access is very simple: it does not provide any real encapsulation. Any other module in the system can define classes in the same package as your complex independent component and get full access to its internals. On the other hand, classes with internal visibility cannot be accessed from any module other than the one where they are defined.

Upvotes: 10

Related Questions