dt1000
dt1000

Reputation: 3732

General programming, abstract class calls method it does not "know" about - AS3

I am working in AS3. We have an abstract class, call it MyAbstractClass (I don't think it is truly abstract, but we are ONLY extending it). The classes that extend this class also implement an interface, call it IMyInterface. IMyInterface has a function called updateView.

I want one of my functions in MyAbstractClass to call updateView, but of course MyAbstractClass doesn't have a function called updateView so it won't compile. Am I attempting a bad-practice type of thing? Is there an easy way to do what I want?

Should I move updateView to MyAbstractClass, and just override it everywhere I need to? thanks

Upvotes: 0

Views: 681

Answers (3)

J. Holmes
J. Holmes

Reputation: 18546

When you want to invoke the interface method in your "abstract" class you could do the following:

if(this is IMyInterface) IMyInterface(this).updateView();

It'll work, but its not really pretty or elegant. You should probably rethink your hierarchy, like you are considering.

Upvotes: 0

Rob Skelly
Rob Skelly

Reputation: 211

From the sounds of things, all of your classes, including the abstract class need to implement IMyInterface, so it should be at the top of the heirarchy. Of course, those classes that extend the abstract class don't need to explicitly declare that they implement IMyInterface, but conceptually they do.

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1502116

Yes, it absolutely sounds like this should be an abstract method within MyAbstractClass. If all the implementations also implement IMyInterface, it sounds like you should possibly declare that MyAbstractClass declares it too. Of course, these will both require that you make MyAbstractClass properly abstract.

Basically, you need to ask yourself: logically, should you be able to treat every instace of MyAbstractClass as an IMyInterface? Should they all have updateView? Does that make sense for MyAbstractClass, or are they logically separate?

Upvotes: 2

Related Questions