Skip to main content
Known Participant
April 20, 2006
Question

super and Polymorphism

  • April 20, 2006
  • 1 reply
  • 214 views
I have a class called Player and a subclass that extends player called CompPlayer. Here is my code for the player class (only that which relates to this topic):

class Player
{
//Class properties
//Private Members
private var m_PicFile:String;

//Class Methods
public function Player(picFile:String)
{

this.m_PicFile = picFile;

}

public function getPicName():String
{
trace("HERE I AM");
return this.m_PicFile;
}

}

The CompPlayer class looks like:
class CompPlayer extends Player
{
//Class properties
//Private Members
private var m_PlayStyle:Number;

//Class Methods
public function CompPlayer(picFile:String, playStyle:Number)
{
super(picFile, name, age, gender);
this.m_PlayStyle = playStyle;
}

public function getPicName()
{
super.getPicName();
}


}


I then instantiate a CompPlayer:
var comp:CompPlayer = new CompPlayer("picture.png", 2);

I then run the following code:
comp.getPicName();

In the debug screen, I get "HERE I AM" and "undefined". This is the problem. It calls the correct method, but cannot access the variable. If I delete the getPicName() inside of the CompPlayer class, it works just fine and outputs "HERE I AM" and "picture.png". Anybody know what is going on here? Why can't I access the m_PicFileName using the super.getPicName() from the CompPlayer class?
This topic has been closed for replies.

1 reply

Inspiring
April 20, 2006
Don't know where the undefined comes from -- you must have omitted some
code. I made a couple minor changes (see below) and it works for me.

<SNIP>

> //Class Methods
> public function CompPlayer(picFile:String, playStyle:Number)
> {
> super(picFile, name, age, gender); // error here -- will not
compile

super(picFile)

> this.m_PlayStyle = playStyle;
> }
>
> public function getPicName()
> {
> super.getPicName();
return super.getPicName();
> }
>
>
> }
>
>
> I then instantiate a CompPlayer:
> var comp:CompPlayer = new CompPlayer("picture.png", 2);
>
> I then run the following code:
> comp.getPicName();

trace(comp.getPicName());

OUTPUT:
HERE I AM
picture.png