I have the following code in my Typescript. But it reports the following error in the child._moveDeltaX(delta)
line:
ERROR: Property '_moveDeltaX' is protected and only accesible
through an instance of class 'Container'
INFO: (method) Drawable._moveDeltaX( delta:number):void
The code is the folowing:
class Drawable {
private _x:number = 0;
constructor() {}
/**
* Moves the instance a delta X number of pixels
*/
protected _moveDeltaX( delta:number):void {
this._x += delta;
}
}
class Container extends Drawable {
// List of childrens of the Container object
private childs:Array<Drawable> = [];
constructor(){ super();}
protected _moveDeltaX( delta:number ):void {
super._moveDeltaX(delta);
this.childs.forEach( child => {
// ERROR: Property '_moveDeltaX' is protected and only accesible
// through an instance of class 'Container'
// INFO: (method) Drawable._moveDeltaX( delta:number):void
child._moveDeltaX(delta);
});
}
}
What do I have wrong? I thought that you can access to a protected method. In other lenguajes this code would work without problem.
You "childs" object is not in in visibility of the inherited object. You just made a new instance that cannot access the protected method. You can access the protected methods using super, but not for other instances.
This would fail in c# as well (I think).