Member-only story

Kotlin — Unit Testing Classes Without Leaking Public API!

Dude, where’s my package-private access modifier?

Stanislav Kozlovski
6 min readMay 14, 2020
Photo by NeONBRAND on Unsplash

Introduction

Kotlin is an amazing new up-and-coming language. It’s very actively developed and has a ton of features that make it very appealing.
It’s been steadily gaining market share ever since Google added Android Development support for it (back in 2017) and made it the preferred language for such development exactly one year ago (May 2019)

For any Java developer coming into the language, spoiler alert, there is one big surprise that awaits you — the package-private visibility modifier is missing. It doesn’t exist.

Access Modifiers

An access modifier is the way to set the accessibility of a class/method/variable in object-oriented languages. It’s instrumental in facilitating proper encapsulation of components.

Java

In Java, we have four access modifiers for any methods inside a class:

  • void doSomething()- the default modifier, we call this package-private. Any such declarations are only visible within the same package (hence the name, private for the package)
  • private void doSomething() — the private modifier…

--

--

Stanislav Kozlovski
Stanislav Kozlovski

Written by Stanislav Kozlovski

A generally curious individual — software engineer, mediterranean dweller, regular gym-goer and coffee lover

Responses (1)