Question about static methods and inheritance


#1

I have a question concerning the code below:

open class A() {   class object {   fun foo() {            print("A")   }   } }

class B() : A() {
  class object {
  fun foo() {
           A.foo() // compiles and prints “AB”
           super.foo() // does not compile
           print(“B”)
  }
  }
}

fun main(args: Array<String>) {
  B.foo()
}


Would be nice if super.foo() in B’s class object would compile. But it doesn’t as in Java. This was always something that annoyed about Java. When I change the hierarchy I have to go into some refactoring work. Astonishingly, noby outside the Smalltalk world ever complained about this. Maybe people see this as natural, because it is the same way in C++. I can understand that “myClass allSubclassesDo: aBlock” as in Smalltalk doesn’t work in Java as not all existing subclasses may be loaded into the code space when iterating over all subclasses and calling static methods. But the other way round in the hierarchy is a different thing.

This is not too important, I know. But it would make things a little more intuitive. Another thing are class instance variables: for every subclass some class variable has a different contents as if every subclass had a new instance of that inherited class variable (hence the name). Also, nobody outside the Smalltalk world things this is a must have, I know. But would be nice … :wink:

Regards, Oliver


What is the advantage of "companion object" vs static keyword
#2

the Java Puzzlers book shows how bad it is when static methods are inherited. It is a consious decision not to have it in the language.


#3

This is the first time I hear about problems with static methods and inheritance. In almost 10 years of Smalltalk development I never ran into a situation that caused problems with this and I never heard of any colleague at work about problems with it.

Maybe there are pitfalls when both, static methods and static variables, are no true class methods and class variables as they aren’t in Java (unlike Smalltalk). Strange, but not an important issue.