user3791691
user3791691

Reputation: 75

Private Functions Being Inherited

This is pretty self explanatory, but My code in the Subclass keeps inheriting private functions in the super class, I believe this is a very big problem.

Do you think you can decode why the private function on my code is being inherited into the subclass from the super class?

Super Class:

package  {

    import flash.display.MovieClip;
    import flash.events.Event;

    public class Level1 extends MovieClip {

        public static var rightSide:Boolean;
        public static var leftSide:Boolean;

        public function Level1() {
            // constructor code
            stage.addEventListener(Event.ENTER_FRAME, loop)
        }
        public function loop(e:Event){
            trace("x is" + x)
            endofGround();
            hhh();
            //if they are left or right move the ground in proportion to their speed
            if(rightSide){x-=Okechuku.speedX; trace("right")}
            if(leftSide){x+=Okechuku.speedX; trace("left")}
        }
        private function endofGround(){
            //if((x>=1720.35) || (x<=-81.5)){trace("LOOOL");}
            if(x>=1720.35){trace("TOO FAR LEFT");}
            if(x>=1){trace("TOO FAR RIGHT");}
            scaleY=2

        }
        private function hhh():void{
            rotation+=1;
        }
    }

}

Subclass:

package  {

import flash.display.MovieClip;
import flash.events.MouseEvent;
import fl.transitions.Tween;
import fl.transitions.TweenEvent;
import fl.transitions.easing.*;


public class PassPort extends Level1 {



    var shrink:Tween;
    public function PassPort() {
        // constructor code
        super();
        addEventListener(MouseEvent.MOUSE_OVER, highLight)
        addEventListener(MouseEvent.MOUSE_OUT, unhighlight)
        addEventListener(MouseEvent.CLICK, gotit)
    }
    function highLight(me:MouseEvent){
        pp.gotoAndStop(2);
    }
    function unhighlight(me:MouseEvent){
        pp.gotoAndStop(1);
    }
    function gotit(c:MouseEvent){
        shrink = new Tween(this,"scaleX", Regular.easeInOut,1,0,20)
        shrink.addEventListener(TweenEvent.MOTION_FINISH, removePp)
        removeEventListener(MouseEvent.CLICK, gotit)
    }
    function removePp(t:TweenEvent){
        MovieClip(root).objtxt.text = "Objective: Get to the Airport.";
        MovieClip(parent).removeChild(this);
    }

}

}

Upvotes: 2

Views: 136

Answers (3)

BadFeelingAboutThis
BadFeelingAboutThis

Reputation: 14406

When you inherit a class, all the methods and variables from the super class still exist (no matter how they are declared). If they are declared as private, they simply become inaccessible to sub class.

The base class constructor still runs, and if it calls private functions they still execute.

If you want to change something from happening in the sub class, then you need to override that method (which then replaces the old one - though the old one can still be accessed with the super keyword).


EDIT

Here is an example you may find useful

public class BaseClass {
    public function BaseClass(){
        init();
    }

    protected function init():void {
        myPrivateFunction();
    }

    private function myPrivateFunction():void {
        trace("PrivateFunction!!!");
    }
}

public class SubClass1 extends BaseClass {
    public function SubClass1(){

    };
}

public class SubClass2 extends BaseClass {
    public override init():void{
        trace("No private function running here");
    }
}

In the example above, instantiating a SubClass1 would results in a trace of "PrivateFunction!!!" - because the base class constructor still runs and executes it's code private or otherwise.

instantiating a SubClass2 however, would result in a trace of No Private Function running here", because it overrides the init() method which now no longer calls the private function from the base class. In the SubClass2, you could also call super.init() to still get the base class method to run (which then calls the private method which outputs the original trace).

Upvotes: 3

BotMaster
BotMaster

Reputation: 2223

You misunderstand the use of private method. They will always be present in the super class and eventually run if called internally. If you want to prevent a private method from running then it should not be private but protected. protected methods are like private methods except that they are inherited and as such can be overrided. Overriding a method allows you to implement a different functionality or to turn that method off completely (you override but add no code = method is off).

Upvotes: 0

Fygo
Fygo

Reputation: 4665

The private methods are not inherited. They are existent in the super class but not accessible in the subclass.

Private methods/vars from the super class don't just magically disappear. What do you think would happen if you had this in your super class:

public function doSomethingPublic():void {
   doSomethingPrivate();
}

private function doSomethingPrivate():void {
  //do something
}

And then call super.doSomethingPublic() from your subclass? Now that would be a very weird scenario... Should it throw an error or what should happen? Of course it shouldn't. It will simply call doSomethingPrivate() as this function exists in the super class.

Upvotes: 1

Related Questions