Internal, protected, public, private 101

Public, internal, protected methods should be marked “override” in a derived class when be ovverriden. Also the visibility and signature of these methods cannot be changed by the derived class.

Static and private methods are hiding from the outside world.
So a derived class is unaware of its parent class’ static/private members, which means you can create static or private methods with the same name but different signatures in the derived class than the static/private methods in its parent classes.

Static methods are treated differently than non-static ones.
A static method can co-exist with an instance method with the same name. The signature can also be different.

Example:

package test
{
import flash.display.Sprite;

public class InheritanceTest01 extends Sprite
{
public function InheritanceTest01()
{
var a1:A1 = new A1();
//a1.sayHi(“we should go beers”, “have fun”);
//a1.walk(12);
A1.sayHi();
}
}
}

class A
{
private function sayHi():void
{
trace(“hi!”);
}

protected function walk(distance:Number):void
{
trace(“I will walk for “+distance+” miles.”);
}

public function run():void
{
trace(“I run!”);
}

internal function sleep(duration:Number):void
{
trace(“I am going to sleep for “+duration+” hours.”);
}
}
class A1 extends A
{
public function A1()
{
super();
sayHi(7);
}
/*
public function sayHi(w1:String, w2:String):void
{
trace(“Oh hi! I am just saying “+w1+” and probably “+w2+”.”);
walk(12);
run();
sleep(1.5);
}
*/
private function sayHi(times: uint):void
{
if(!times)return;
for(var i:uint =0 ; i<times; i++)
{
trace(“hi”);
}
}
static function sayHi():void
{
trace(“Ciao!”);
}

override protected function walk(distance:Number):void
{
trace(“I will walk for “+distance/2+” miles and then another “+distance/2+” miles.”);
}

override public function run():void
{
super.walk(1);
trace(” then “);
super.run();
}

override internal function sleep(duration:Number):void
{
super.sleep(duration);
trace(“and I am going to sleep for another “+duration+” hours!”);
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: