What are the differences between Scala s inner classes and Javas inner / nested classes?

How does Scala handle inner classes differently with Java nested, static or non-stationary classes?

+4
source share
2 answers

The main difference is that if you have

class Outer {
  class Inner {
    def foo(x: Inner): Inner = this // just for the example below
  }
}

and two instances Outer:

val a = new Outer
val b = new Outer

then a.Innerand b.Inner- two different types (which in Java they would be Outer.Inner), so you can not do

val aInner = new a.Inner
val bInner = new b.Inner
aInner.foo(bInner)

They have a common supertype that is written Outer#Inner.

+6
source

Scala , , -. Java . , , . IOW: , , (), , .

IOW, foo.SomeInnerClass bar.SomeInnerClass - , .

+2

Source: https://habr.com/ru/post/1655749/


All Articles