-1

I am not able to understand why Inner Class in Scala is bound to Outer Class Object and not to Outer Class itself like in Java ?. Can someone explain any logic behind it. Why Scala developers defined Inner Class in this way?

Please see example below

class Network
{
  class Member()
  {
    val contacts = new ArrayBuffer[Member]
  }
}


object checkInner extends App {

/// creating home network  
  val home = new Network
  val father = new home.Member
  val mom = new home.Member
  val sister = new home.Member

  father.contacts += mom
  father.contacts += sister

// creating neighbour network
  val neighbour = new Network
  val uncle = new neighbour.Member

  father.contacts += uncle  (this will throw error -> type mismatch; found :    
       checkInner.neighbour.Member required: checkInner.home.Member)
       ** but it will work in Java **
}
jack
  • 355
  • 4
  • 11
  • Do you realize that Java is exactly like that? The only difference is that Java supports static members, including static inner classes. – pedrofurla Feb 17 '17 at 16:34
  • What exactly are you trying to suggest. Can you please elaborate on it. Thanks – jack Feb 17 '17 at 16:42
  • I asked one yes or no question and made a statement. I don't know how to be more explicit than that. – pedrofurla Feb 17 '17 at 17:24
  • If you added example code in both languages, I think you could get a clearer answer. @pedrofurla is stating that non-static inner classes in Java are bound in instances of the class, which seems to be your question. – jkinkead Feb 17 '17 at 17:35
  • What @pedrofurla means to say that inner classes in Java are defined as `static` and a `class` can have static members. Where as in Scala, a `class` can not have any static members and all static members must go to the companion object. And if you are asking `why ?`... that is because allowing static members is actually against the concept of `pure object oriented programming`, where classes are just supposed to be blue-prints for state and behaviour of some instance. – sarveshseri Feb 17 '17 at 17:45
  • I didn't say that "inner classes in Java are defined as static". They *can* be defined as static. – pedrofurla Feb 17 '17 at 18:50
  • And again, in Scala members of objects or companion objects are not static. – pedrofurla Feb 17 '17 at 18:51
  • I know if you wish to make anything static in Scala, you have to put that in Companion Object.I understand that in Java inner class are bound to outer class( which is implicitly static ) but in Scala, inner class are not bound to outer class instead they are bound to objects of outer class. Still not able to understand why in Scala inner class are bound to outer class objects and not to outer class – jack Feb 17 '17 at 19:09
  • "if you wish to make anything static in Scala, you have to put that in Companion Object" this is not true. Scala simply doesn't have static members. – pedrofurla Feb 17 '17 at 19:24
  • "I understand that in Java inner class are bound to outer class( which is implicitly static ) but in Scala, inner class are not bound to outer class instead they are bound to objects of outer class." This is incorrect. Non static inner classes in Java have the exact same semantics as in inner classes in Scala. Hence my original question. I am adding an answer clarifying these points. – pedrofurla Feb 17 '17 at 19:29
  • 2
    It would really, really help if you asked your question using code. _Show_ us what the difference (that you say exists) between Java and Scala is that you want to ask about. – Seth Tisue Feb 17 '17 at 19:36
  • @pedrofurla, Thanks for prompt response. Here i am writing an excerpt taken from Scala Cookbook by Alvin Alexander. It states - **The concept of a “class within a class” is different in Scala than in Java. As described on the official Scala website, “Opposed to Java-like languages where such inner classes are members of the enclosing class, in Scala, such inner classes are bound to the outer object**. This has created a lot of confusion for me. – jack Feb 17 '17 at 19:42
  • Ah, so the question is about http://docs.scala-lang.org/tutorials/tour/inner-classes.html... you should have said so from the beginning... – Seth Tisue Feb 17 '17 at 20:05
  • Sorry mate. My bad. Can you please clear my doubt. You can go through the code i have stated above. – jack Feb 17 '17 at 20:07
  • 1
    Thanks for helping us @SethTisue – pedrofurla Feb 17 '17 at 20:10

1 Answers1

1

Non static inner classes in Java have the exact same semantics as in inner classes in Scala. (take that with a grain of salt, the difference in the type systems of both languages may introduce some things that invalidates that statement but these are irrelevant to what I am about to explain).

Both in Java a inner (non-static) class contains a reference to the instance of the outer class that was used to create it. The syntax for instantiation of a inner class makes it very clear (Assume that the for the java case HashPerson is non-static inner class in ContainingClass):

ContainingClass container = new ContainingClass();
HashPerson william = container.new HashPerson("willy");

(taken from https://stackoverflow.com/a/4070777/480674)

notice how container is on the left of the .new, as if this .new were a member of container. In Scala same would be new container.HashPerson("willy"); and in both languages the HashPerson instance would depend on a instance of the outer class.

Roughly the only real difference between the two language regarding inner classes (that are non-static in the Java case) is that Scala has the concept of path-dependent types. But that is entire different answer.

Community
  • 1
  • 1
pedrofurla
  • 12,753
  • 1
  • 37
  • 49
  • 1
    I believe the language at http://docs.scala-lang.org/tutorials/tour/inner-classes.html that the question was about (as we finally found out in the comments above) is actually about path-dependent types specifically. (That page needs an overhaul, IMO; it's about path-dependent types but never uses those words.) – Seth Tisue Feb 17 '17 at 20:08
  • Yeah, I realized that now. – pedrofurla Feb 17 '17 at 20:10
  • And unfortunately, I'm not sure where the best explanation of path dependent types is. http://stackoverflow.com/questions/5581836/why-does-scala-have-path-dependent-types?rq=1 and http://stackoverflow.com/questions/2693067/what-is-meant-by-scalas-path-dependent-types?rq=1 are not that great. – Seth Tisue Feb 17 '17 at 20:12
  • Thanks @SethTisue for directing on right way. – jack Feb 17 '17 at 20:18
  • I vaguely remember liking the one in "An overview of Scala Programming Language" https://infoscience.epfl.ch/record/52656/files/ScalaOverview.pdf, but I suppose this is ancient by now. – pedrofurla Feb 17 '17 at 20:34
  • The really short answer is: that's how they work in Beta, where nested classes were invented. – Jörg W Mittag Feb 19 '17 at 02:28