PDA

View Full Version : [AS3] Subclass Method Return Type



Krilnon
July 19th, 2006, 01:42 PM
I am extending the Array class. Many of its methods return Array objects, but I would like them to return instances of my subclass. I only need to change the return type (and the argument if it is also of the Array class), as the method does just what I want it to otherwise.

Overriding the method won't work, as the return type has to be the same. Attempting to use a method that returns an Array to assign the value of a subclass of Array generates an error, how can I fix this? (Or is more detail needed?)

senocular
July 19th, 2006, 02:27 PM
Thats a good question - one I don't think I have the answer to. As far as I know, you can't, and I've been struggling with this as well, especially with clone methods.

The problem with clone methods is that if you try to use super.clone(), what you get is a clone with the type of the superclass and not your subclass. You cant even 'as'-it to be an instance of your subclass because that clone is essentially an instance of the superclass... not to mention since you are overriding the clone method, the method signature has to be the same meaning you're returning an instance of the superclass regardless. At that point it tends to make more sense to use composition instead of inheritance.

Krilnon
July 19th, 2006, 02:58 PM
Thanks for clearing that up. :)

Could you explain what you mean by 'composition'? I'm trying to guess whether you mean mathematical function composition, composing my own function to act like the one that I can't override as I would like, or perhaps something else completely. Sorry if the meaning should be obvious. :-/

senocular
July 19th, 2006, 06:50 PM
;) composition is a term used in OOP to describe "containing" an instance of a class rather than inheriting from it. So, for example, you can have an ExtendedPoint class which inherits from the Point object, or you can have it not inherit from anything and, instead, keep a private instance of a Point which you use for all your point-specific methods. Then, however, you have to re-write all your point methods, but instead of writing them completely from scratch, you just call them through your point property.

public function interpolate(pt1:Point, pt2:Point, f:Number):Point {
return myPointProperty.normalize(pt1, pt2, f);
}
etc.

Doing this also means you don't have to override meaning you're welcome to change the method signature and have it return an ExtendedPoint instance instead of a Point instance if you wanted. The problem is then, however, your ExtendedPoint class is no longer of the Point type and can't be used in place of Points where points are expected (though a workaround to that is making the private composed point public and using that instead).

Some methods/classes use interfaces instead of specific Class types for values they expect. For example, instead of having a parameter typed as EventDispatcher, you should type it with IEventDispatcher, which is the EventDispatcher interface. This means that the parameter includes EventDispatcher instances and any class that implements that interface. And what THAT means is that you won't have to inherit from EventDispatcher to be able to use a class instance for that parameter, just implement that interface. The example with EventDispatcher in the AS3 tips thread shows an example of this. And you can see there how methods were created and just wrapping those calls made to the eventDispatcher instance "composed" in that class :)

Jeff Wheeler
July 19th, 2006, 06:54 PM
Hmm… I never have heard that term composition in the context of OOP, however, I've used it lots. Is it a common term outside of Flash?

senocular
July 19th, 2006, 07:16 PM
yeah, its pretty common. Essentially it describes the "has a" relationship as opposed to the "is a" relationship which is specific to inheritance.

Jeff Wheeler
July 19th, 2006, 07:19 PM
Heh, thanks. :)

Krilnon
July 20th, 2006, 05:58 PM
Thanks senocular. I've given my class a public Array instance and it's working well so far.

senocular
July 20th, 2006, 06:04 PM
Thanks senocular. I've given my class a public Array instance and it's working well so far.

I'll see what I can't dig up about this...

klick175
July 20th, 2006, 08:49 PM
The easiest thing to do would be to overload the method. If you're not familiar with OOP lingo, that means to create a method in a subclass with the same name of a method in its superclass but with a different signature.

I've heard this is possible in AS2, though not as straightforward as it is in other OOP languages (e.g. Java). Allegedly, AS3 uses the traditional syntax for overloading. If you're still grappling with this, I can try to find out how to overload methods in AS2, assuming it's indeed possible. Let me know.

senocular
July 20th, 2006, 09:04 PM
Overloading is not supported in AS3, nor is it in AS2, though it is possble in AS2 to override an inherited method with a different signature. In AS3, having a method with the same name as that belonging to a superclass implies overriding and an error will be thrown if you do not override it with the same signature (or without using the override keyword).

Krilnon
July 20th, 2006, 09:15 PM
I'm familiar with overloading, but as senocular just mentioned, it won't work.

Not to distract you from your digging, senocular, but is what I mentioned doing in post #8 similar to what is going on in the Sprite class, where every Sprite instance has a Graphics object?

klick175
July 20th, 2006, 09:47 PM
Weird. I had read somewhere that overloading would be supported in AS3. Just Google'd and you're right, it's not. My bad. I had bad information.


Overloading is not supported in AS3, nor is it in AS2, though it is possble in AS2 to override an inherited method with a different signature. In AS3, having a method with the same name as that belonging to a superclass implies overriding and an error will be thrown if you do not override it with the same signature (or without using the override keyword).

senocular
July 20th, 2006, 10:21 PM
Not to distract you from your digging, senocular, but is what I mentioned doing in post #8 similar to what is going on in the Sprite class, where every Sprite instance has a Graphics object?

More or less, yeah.

xavii
March 17th, 2008, 11:44 PM
I am extending the Array class. Many of its methods return Array objects, but I would like them to return instances of my subclass. I only need to change the return type (and the argument if it is also of the Array class), as the method does just what I want it to otherwise.

Overriding the method won't work, as the return type has to be the same. Attempting to use a method that returns an Array to assign the value of a subclass of Array generates an error, how can I fix this? (Or is more detail needed?) :)