Hi. Long time programmer, new to smalltalk.
I'm trying to do the simplest thing, but there is clearly some basic concept that I'm missing here. I'm running Squeak 3.6.
In short, I have ClassA and ClassB, which is a subclass of Class A. Each class has one variable, and when I create the class I'd like to see the initialization methods run on both classes.
When I execute:
b _ ClassB new initialize
I expected that both classes would be initialized. What actually happens is that only ClassB is initialized.
Here is the source code. Can anybody tell me what I'm doing wrong?
-------
Object subclass: #ClassA instanceVariableNames: 'anIntA ' classVariableNames: '' poolDictionaries: '' category: 'SEG'
initialize anIntA _ 1.
new ^ super new initialize
-------
ClassA subclass: #ClassB instanceVariableNames: 'anIntB ' classVariableNames: '' poolDictionaries: '' category: 'SEG'
initialize anIntB _ 1
new ^ super new initialize
Steve,
Steve Greenberg wrote:
...
Here is the source code. Can anybody tell me what I'm doing wrong?
Object subclass: #ClassA instanceVariableNames: 'anIntA ' classVariableNames: '' poolDictionaries: '' category: 'SEG'
initialize anIntA _ 1.
new ^ super new initialize
ClassA subclass: #ClassB instanceVariableNames: 'anIntB ' classVariableNames: '' poolDictionaries: '' category: 'SEG'
initialize anIntB _ 1
You have to change this method to
initialize super initialize. anIntB _ 1
This is the way to call an overridden method in a superclass (and should mostly be done this way in >>initialize methods).
new ^ super new initialize
Note: this class method calls the overridden >>new in the superclass ClassA, and the overriden >>new class method in the superclass ClassA calls the >>initialize instance method in ClassB (not the one in ClassA!). And the last one should call its overridden super >>initialize in ClassA.
Greetings Stephan
Welcome to Squeak!
On Monday 28 June 2004 6:38 pm, Steve Greenberg wrote:
I'm trying to do the simplest thing, but there is clearly some basic concept that I'm missing here. I'm running Squeak 3.6.
Note that Squeak 3.7 has an important change: there is a default call to #initialize in the definition of #new up in Behavior.
So you will not often want to write this kind of code any more (as it's redundant):
MyClass class >> new ^super basicNew initialize
In short, I have ClassA and ClassB, which is a subclass of Class A. Each class has one variable, and when I create the class I'd like to see the initialization methods run on both classes.
Are you talking about class initialization (like when you load it into a new image) or instance initialization (like when you make a new object of your class)? We have both flavors, since classes are also objects.
When I execute:
b _ ClassB new initialize
I expected that both classes would be initialized. What actually happens is that only ClassB is initialized.
I hope that your new object ("b") is what's getting initialized, *not* the class.
What should happen, given your code, is:
ClassB class >> new ClassA class >> new Object class >> new Object class >> basicNew ClassB>> initialize ClassB>> initialize
That is, ClassB>>initialize gets called twice, and the ClassA>>initialize gets called not at all.
Object subclass: #ClassA instanceVariableNames: 'anIntA ' classVariableNames: '' poolDictionaries: '' category: 'SEG'
initialize anIntA _ 1.
new ^ super new initialize
This isn't really a safe scheme; if you later make a new superclass for ClassA that *itself* calls initialize, you'll get it called again. Better probably to say ^super basicNew initialize
and to make your initialize method call *its* superclass:
initialize super initialize. anIntA _ 1.
ClassA subclass: #ClassB instanceVariableNames: 'anIntB ' classVariableNames: '' poolDictionaries: '' category: 'SEG'
initialize anIntB _ 1
If you want the superclass initialize method to be called, you should also do it here:
initialize super initialize. anIntB _ 1
new ^ super new initialize
ClassB class>>new isn't needed (it's a duplicate of its parent's definition). And in fact it's causing ClassB>>initialize to be called twice.
squeak-dev@lists.squeakfoundation.org