4

The Wikipedia article on the subject of inner classes enumerates a number of programming languages that support nested class definitions. Historically speaking, which programming language first added nested classes to its feature set?

  • 5
    Citing the very link you posted: “BETA language introduced this notion of nested classes.” – amon Jun 25 '18 at 15:36
  • It’s worth noting that the list in that article is woefully incomplete. Virtually all languages that support ad-hoc class declarations support inner classes, including LISP dialects and many functional programming languages. – Konrad Rudolph Jun 27 '18 at 9:12
  • 2
    @KonradRudolph: OTOH, the list includes Ruby, in which you can have lexically nested class definitions, but they don't produce nested classes. They only produce nested names. The list seems to confuse the syntactic possibility of writing a class inside another class with the semantic feature of nested classes, which are properties of the class instance just like fields are. In fact, a nested class is in essence very much like a first-class class object assigned to a field of the object. – Jörg W Mittag Jun 27 '18 at 12:29
  • 1
    @JörgWMittag Yes, same for most of the list, including C++, VB, C#. That article is rubbish. – Konrad Rudolph Jun 27 '18 at 12:40
  • Another, equally interesting pioneering innovation by Beta are virtual classes. In fact, they don't make sense without "true" nested classes, since a virtual class is a class that is a member of an outer class (IOW a nested class) that can be overridden in a subclass, just like a virtual method is a member of a class that can be overridden in a subclass. They are insanely powerful and exist AFAIK only in Beta, gBeta, Newspeak and some research dialects of Java. Scala tried them out, but they were so powerful that they broke the type system, so they were removed again. – Jörg W Mittag Jun 27 '18 at 20:57
9

The first language with nested classes was Beta, a successor to Simula. They are carried even further in Beta's successor, gBeta. A modern language that carries them quite far is Newspeak. Scala's nesting also works very similar to Beta's.

Java's inner classes are inspired by Beta's but (at least according to some people who know Beta quite well and were also involved in the design of Java) they don't fully capture all the features. (I am quoting quite liberally here, I think the actual quote was something more like "the people who designed Java tried copying nested classes without understanding them".)

Note that in your question, you use the term "nested class definitions", which is ambiguous: It can mean "nested definitions of classes" or it can mean "definitions of nested classes". In my answer, I am assuming you mean the latter, since you explicitly call out Java's inner classes, which are definitions of nested classes (with some caveats).

There are lots of languages that allow you to nest a class definition inside another class definition, but only in very few of those languages does this actually produce a nested class. For example, in Ruby:

class Foo
  class Bar; end
end

does not produce a nested class. There is no relationship between the two classes and/or between the class Bar and instances of class Foo. The only thing this does is bind class Bar to a constant named Bar namespaced inside Foo. The Wikipedia article is simply wrong in including Ruby in the list. At the top, the article defines meaning #1 above, but the list is actually languages supporting meaning #2.

A proper nested class has the following properties:

  • The inner class is a property of instances of the outer class, i.e. the "nested" class is not actually nested inside the outer class, it is nested inside instances of the outer class, which also means that each instance of the outer class gets its own copy of the inner class (at least semantically). This means that a.Inner and b.Inner (assuming a and b are instances of Outer) are not the same class! (And the class Outer.Inner doesn't exist.)
  • Likewise, instances of the inner class are nested inside their corresponding instances of the outer class, they are visible only to that single object (unless exported, i.e. returned from a public method).

As Konrad Rudolph pointed out in his comment, being able to lexically nest class definitions is a simple and obvious thing that is not interesting in any way.

| improve this answer | |
  • So, the only thing instances of inner classes have that other classes don't is a back-pointer, and you consider that part of the classes identity, instead of the objects state? Did I miss anything there? – Deduplicator Jun 28 '18 at 0:19
-1

C++ has nested classes, and pretty much from day one

| improve this answer | |
  • 4
    Those are fundamentally different from Java’s inner classes, which share the state of the parent class instance. It’s not quite clear but I’m assuming that that’s what OP is asking about since C++’s inner classes are a boring concept that requires no further discussion. – Konrad Rudolph Jun 27 '18 at 9:10
  • @KonradRudolph Do Java nested classes have any interesting properties aside from an implicit back-pointer to the instance of the outer class? – Deduplicator Jun 27 '18 at 22:08
  • @Deduplicator Do closures have any interesting properties aside from an implicit back-pointer to the instance of the enclosing scope? — In both cases the answer is “no” but it’s missing the emergent properties you get from that. – Konrad Rudolph Jun 28 '18 at 9:42
  • @KonradRudolph: Often, they don't have that at all (Lua closures only know the variables they use, C++ ones only those the programmer asked for). And which emergent properties? – Deduplicator Jun 28 '18 at 11:22
  • @Deduplicator Right but that’s an implementation detail. As for emergent properties, everything that can be accomplished with closures. There’s rich theory there, and even richer practice (OOP is a consequence of closures). – Konrad Rudolph Jun 28 '18 at 12:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.