views:

508

answers:

3

I have found some code that uses the virtual keyword for functions, like:

package tryOut{
    public class Parent {

        public function Parent() {}

        public function foo():void{
            trace("Parent foo");
        }//foo

        public virtual function bar():void{
            trace("Parent virtual bar");
        }//bar      
    }//class
}//package

As far as I understand using the virtual keyword should modify the way overriding a method works, or the way using a child method would work, or something. But it seems it does nothing at all. Having the extention:

package tryOut {
    public class Child extends Parent {
        public function Child() {}

        public override function foo():void {
            trace("Child foo");
        }//foo

        public override function bar():void {
            trace("Child virtual bar");
        }//bar

    }//class
}//package

The following code prints:

var parent:Parent = new Parent();
var child:Child = new Child();

parent.foo(); //Parent foo
child.foo(); //Child foo
parent.bar(); //Parent virtual bar
child.bar(); //Child virtual bar

var childCast:Parent = child as Parent;

parent.foo(); //Parent foo
childCast.foo(); //Child foo
parent.bar(); //Parent virtual bar
childCast.bar(); //Child virtual bar

So both methods work the same regarding the override. Does the virtual keyword changes something I am missing?

+1  A: 

So both methods work the same regarding the override.

What makes you think that? The tests you've shown aren't comparable.

childCast is typed as a Parent, yet you still end up calling the function in Child.

You don't check the same situation for the non-virtual method.

Anon.
My bad, I corrected the example. Even so, the behavior keeps the same.
LopSae
A: 

The virtual keyword is ignored by the compiler and is just a placeholder for possible future implementation.

!EDIT: INCORRECT, this does work if you test it! If it were possible to use virtual functions then you could make use of true polymorphism.

For example:

class Bird
{
   virtual function communicate();
}

class Parrot extends Bird
{
  override virtual function communicate();
}

It would then be possible to do this:

var myBird:Bird = new Parrot();
myBird.communicate();//this would call the Parrot's implementation
Allan
Methods are already virtual in AS3, so "true polymorphism" is possible. (For the virtual keyword to make sense, methods would be non-virtual by default and I highly doubt AS will go down that path).
I am idiot, how embarrassing. I could have sworn I tested this a year ago and found that it did not work. Tried it again just then and yes I have polymorphism. In fact I have already been using it with Interfaces... :S oh well all good, thanks
Allan
+6  A: 

From the help documents (If you're using Flash, do a search for 'virtual'):

There are also several identifiers that are sometimes referred to as future reserved words. These identifiers are not reserved by ActionScript 3.0, though some of them may be treated as keywords by software that incorporates ActionScript 3.0. You might be able to use many of these identifiers in your code, but Adobe recommends that you do not use them because they may appear as keywords in a subsequent version of the language.

abstract boolean byte cast

char debugger double enum

export float goto intrinsic

long prototype short synchronized

throws to transient type

virtual volatile

So in AS3, virtual does absolutely nothing.

Wallacoloo
And the actual page is, for anyone interested: http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3_Flex/WS5b3ccc516d4fbf351e63e3d118a9b90204-7f9b.html
LopSae
Thanks LopSae, I actually just happened to have Flash open at the time, so I used the offline help.
Wallacoloo