0

Lets say I am given an object O on some method.

This object O is derived from a base Object BaseClass and as such has a part whose type is BaseClass.

How can I access this part when I am in this method, which means super wont work because I am not in the context of the object.

Thanks!

TomerZ
  • 615
  • 6
  • 17

2 Answers2

3

Let me re-phrase your question to make sure I understand it. You have a class O containing a method (say "test"). In that method, you want to access an instance variable belonging to the superclass BaseClass.

If this is correct, then you can already access that instance variable directly. You just need to provide the name of the variable. Your subclass has access to all of the instance variables visible to the superclass.

You should consider creating get and set methods for the variable and accessing the variable by calling those methods from the subclass, but it's optional.

David Buck
  • 2,847
  • 15
  • 16
1

Let me provide another answer, which could be useful for cases where the question refers to behavior (methods) rather than shape (instance variables.)

Assume you have two classes C and D and an instance c of C. Now assume C inherits from C' and you would like to invoke a method m defined in C' that has been overridden in C. The expression c m will activate the implementation in C rather than the one in C' because c class == C, not C'. As you said, there is no "remote" version of super that you could use from D.

If what you want is to activate the method in C', then you should move the source code of m in C' to another method, say m0, and redefine m in C' so that it just delegates to m0 (^self m0). Keep the method m in C unchanged and then call from D using m0 (c m0) instead of m (c m).

Note that the other way around will not work: if you define m0 in C' as ^self m, the expression c m0 will activate the version of m found in C, not C'.

You could also define m0 in C as ^super m and that way c m0 will activate C'>>m. However, the usage of super with a different selector is not considered a good practice, and you should chose not to do that.

Leandro Caniglia
  • 14,495
  • 4
  • 29
  • 51