# Why outer class cannot see private members of inner class?

**URL:** <https://discuss.kotlinlang.org/t/why-outer-class-cannot-see-private-members-of-inner-class/6791>\
**Category:** Language Design\
**Created:** [February 26, 2018, 6:38pm UTC](https://discuss.kotlinlang.org/t/why-outer-class-cannot-see-private-members-of-inner-class/6791 "2018-02-26T18:38:16Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![baltiyskiy](https://avatars.discourse-cdn.com/v4/letter/b/85e7bf/32.png) [@baltiyskiy](https://discuss.kotlinlang.org/u/baltiyskiy)\
**Post date:** [February 26, 2018, 6:38pm UTC](https://discuss.kotlinlang.org/t/why-outer-class-cannot-see-private-members-of-inner-class/6791/1 "2018-02-26T18:38:16Z")

</div>

What was the reason to diverge from Java in this point? I can’t seem to find the rationale in the docs or on this site.

---

<div class="post-metadata">

**Author:** ![pdvrieze](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/pdvrieze/32/1882_2.png) [@pdvrieze](https://discuss.kotlinlang.org/u/pdvrieze)\
**Post date:** [February 26, 2018, 9:47pm UTC](https://discuss.kotlinlang.org/t/why-outer-class-cannot-see-private-members-of-inner-class/6791/2 "2018-02-26T21:47:41Z")

</div>

Probably because the JVM actually can’t and Java generates appropriate synthetic accessors. Kotlin just tries to limit the idea in the first place.

---

<div class="post-metadata">

**Author:** ![baltiyskiy](https://avatars.discourse-cdn.com/v4/letter/b/85e7bf/32.png) [@baltiyskiy](https://discuss.kotlinlang.org/u/baltiyskiy)\
**Post date:** [February 27, 2018, 10:21am UTC](https://discuss.kotlinlang.org/t/why-outer-class-cannot-see-private-members-of-inner-class/6791/3 "2018-02-27T10:21:45Z")

</div>

Wow, I didn’t know that. Still, why can’t Kotlin do the same? It isn’t that language designers shy away from generating bytecode 🙂

Suppose there’s a tree type and a tree node type. You want to allow modifying node pointers only from the tree code, because it preserves some invariants. In Java, you can simply make the Node a (static) inner class of Tree, make the pointer fields private, have a public getter and then either use a private setter or simply manipulate the pointer from the Tree class. Seems like there’s no good way to do that in Kotlin.

```auto
class Tree {
  private val root = Node()

  fun move(node: Node, after: Node) {
    // Need to manipulate pointers while maintaining invariants.
    // E.g. need to say after.nextSibling = node, but how?
  }

  class Node(
    parent: Node? = null,
    firstChild: Node? = null,
    nextSibling: Node? = null
  ) {
    var parent: Node? = parent; private set
    var firstChild: Node? = firstChild; private set
    var nextSibling: Node? = nextSibling; private set
  }
}
```

---

<div class="post-metadata">

**Author:** ![pdvrieze](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/pdvrieze/32/1882_2.png) [@pdvrieze](https://discuss.kotlinlang.org/u/pdvrieze)\
**Post date:** [February 27, 2018, 12:12pm UTC](https://discuss.kotlinlang.org/t/why-outer-class-cannot-see-private-members-of-inner-class/6791/4 "2018-02-27T12:12:14Z")

</div>

In most cases you can get away with making the type itself private. In your case you’ll have to go the extra way by having a Node interface and a NodeImpl class (which is private). Btw. trees are typically implemented immutable (but indeed don’t have to).

---

<div class="post-metadata">

**Author:** ![fatjoe79](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fatjoe79/32/4221_2.png) [@fatjoe79](https://discuss.kotlinlang.org/u/fatjoe79)\
**Post date:** [February 27, 2018, 5:29pm UTC](https://discuss.kotlinlang.org/t/why-outer-class-cannot-see-private-members-of-inner-class/6791/5 "2018-02-27T17:29:59Z")

</div>

Just declare your node impl as a private top level class in the same file.

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [February 28, 2018, 12:37am UTC](https://discuss.kotlinlang.org/t/why-outer-class-cannot-see-private-members-of-inner-class/6791/6 "2018-02-28T00:37:56Z")

</div>

The point is that in a tree you would want your node to be public but have members which can only be accessed by the tree itself, so your node can not be just private

---

<div class="post-metadata">

**Author:** ![pdvrieze](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/pdvrieze/32/1882_2.png) [@pdvrieze](https://discuss.kotlinlang.org/u/pdvrieze)\
**Post date:** [February 28, 2018, 10:50am UTC](https://discuss.kotlinlang.org/t/why-outer-class-cannot-see-private-members-of-inner-class/6791/7 "2018-02-28T10:50:03Z")

</div>

The solution (admittedly not quite perfect) is to have the following:

```kotlin
class Tree {
  val root: Node = NodeImpl

  fun move(node: Node, after: Node) { /* some impl */ }

  interface Node {
    val parent: Node?
    val firstChild: Node?
    val nextSibling: Node?
  }

  private class NodeImpl(override var parent: NodeImpl? = null,
                         override var firstChild: NodeImpl? = null,
                         override var nextSibling: NodeImpl? = null): Node

}

```

This should work although it doesn’t stop “invalid” nodes from being passed in by users, but node ownership is always a problem in mutable trees (copying to a locally created not is always safer in that sense).

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [February 28, 2018, 12:13pm UTC](https://discuss.kotlinlang.org/t/why-outer-class-cannot-see-private-members-of-inner-class/6791/8 "2018-02-28T12:13:35Z")

</div>

If you are separating your code into different modules you can even use an `abstract class` with an `internal constructor` instead of the interface. That way no one outside of the module can create “invalid” nodes.

---

<div class="post-metadata">

**Author:** ![baltiyskiy](https://avatars.discourse-cdn.com/v4/letter/b/85e7bf/32.png) [@baltiyskiy](https://discuss.kotlinlang.org/u/baltiyskiy)\
**Post date:** [March 1, 2018, 2:38pm UTC](https://discuss.kotlinlang.org/t/why-outer-class-cannot-see-private-members-of-inner-class/6791/9 "2018-03-01T14:38:17Z")

</div>

Oh, I see. I somehow missed that you can override `val` with `var`. Thanks!

As for stopping from passing “invalid” nodes — `Node` can be a `sealed` interface

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [March 1, 2018, 2:40pm UTC](https://discuss.kotlinlang.org/t/why-outer-class-cannot-see-private-members-of-inner-class/6791/10 "2018-03-01T14:40:52Z")

</div>

You’re right. I did forget about `sealed` as I have not used it myself so far 🙂

---

<div class="post-metadata">

**Author:** ![gildor](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/gildor/32/1891_2.png) [@gildor](https://discuss.kotlinlang.org/u/gildor)\
**Post date:** [March 6, 2018, 7:36am UTC](https://discuss.kotlinlang.org/t/why-outer-class-cannot-see-private-members-of-inner-class/6791/11 "2018-03-06T07:36:59Z")

</div>

Kotlin allows to do that but uses different default behavior.  
Inner class in Kotlin by default behave like Java’s class with `static` modifier on inner class. So it’s completely separate class without reference to the parent.  
If you want to get access to parent class like in java without `static` you should add modifier `inner` to Kotlin’s internal class.  
It just forces you to be more explicit for such case and it’s harder to leak parent class accidentally like in java, when you just forgot to mark inner class static, it’s actually not the best practice in most cases

---

<div class="post-metadata">

**Author:** ![jonross](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@jonross](https://discuss.kotlinlang.org/u/jonross)\
**Post date:** [October 22, 2018, 11:03pm UTC](https://discuss.kotlinlang.org/t/why-outer-class-cannot-see-private-members-of-inner-class/6791/12 "2018-10-22T23:03:47Z")

</div>

Gildor : He (and I) want to do the exact opposite of “inner”. Tree wants to modify the internals of Node, but also wants to hang out Node(s) to other clients that look immutable. Only Tree should be able to modify the guts of Node.

The solution given works, but… it’d be nice if the language supported it, instead of having to write boiler plate for this pattern every time.
