Inheritance:
is one of the most powerful technequies of Object Oriented Programming. It refers to the way a child inherits the properties, metthods and events of parent or super class ( also known as parent-child relationship). We can create a new class that behaves like a another class which helps in saving effort and time
Inheritance is used to extend the abiltiy of exisisting class instantiate. Suppose a class Area has methods length() and breath(), class Volume which extends class Area has default methods length() and breath() and in addition to this class Volume can have its own methods like height() etc..
Lets start creating parent class Animal, with a constructor that indicates when an instance of the class is instantiated using a trace statement.
Note : Any class that inherits the Animal class gets all of its methods and interfaces
Download Source :http://flashsource.in/AS3OOPs1.html
Inheritance is used to extend the abiltiy of exisisting class instantiate. Suppose a class Area has methods length() and breath(), class Volume which extends class Area has default methods length() and breath() and in addition to this class Volume can have its own methods like height() etc..
Lets start creating parent class Animal, with a constructor that indicates when an instance of the class is instantiated using a trace statement.
Note : Any class that inherits the Animal class gets all of its methods and interfaces
Download Source :http://flashsource.in/AS3OOPs1.html
Animal.as
package
{
public class Animal
{
// Constructor.
public function Animal( ):void
{
trace("Animal Created");
}
public function walk( ):void
{
trace("Parent Animal : is walking on the ground ")
}
public function Type( ):void
{
trace("Type of Animal ")
}
}
}
package
{
public class Animal
{
// Constructor.
public function Animal( ):void
{
trace("Animal Created");
}
public function walk( ):void
{
trace("Parent Animal : is walking on the ground ")
}
public function Type( ):void
{
trace("Type of Animal ")
}
}
}
Now create subclasses DemosticAnimal and WildAnimal which extend the Animal class.
DemosticAnimal.as
DemosticAnimal.as
package
{
public class DemosticAnimal extends Animal
{
// Constructor.
public function DemosticAnimal( ):void
{
}
//to change the default Type to custom Type ,we will override the Type() method
public override function Type( ):void
{
trace("this is Demostic Animal ")
}
//we can add our own methods also in extended class
public function Trained(){
trace("Demostic aninmals are Trained to respond to the master's command");
}
}
}
{
public class DemosticAnimal extends Animal
{
// Constructor.
public function DemosticAnimal( ):void
{
}
//to change the default Type to custom Type ,we will override the Type() method
public override function Type( ):void
{
trace("this is Demostic Animal ")
}
//we can add our own methods also in extended class
public function Trained(){
trace("Demostic aninmals are Trained to respond to the master's command");
}
}
}
WildAnimal.as
package
{
public class WildAnimal extends Animal
{
// Constructor.
public function WildAnimal( ):void
{
}
//to change the default Type to custom Type ,we will override the Type() method
public override function Type( ):void
{
trace("this is Wild Animal ")
}
}
}
{
public class WildAnimal extends Animal
{
// Constructor.
public function WildAnimal( ):void
{
}
//to change the default Type to custom Type ,we will override the Type() method
public override function Type( ):void
{
trace("this is Wild Animal ")
}
}
}
Now create Main class
Main.as
Main.as
package
{
import flash.display.Sprite;
public class Main extends Sprite
{
public function Main( ):void
{
var animal1:DemosticAnimal=new DemosticAnimal( );
animal1.walk( );
animal1.Type( );
animal1.Trained( );
var animal2:WildAnimal=new WildAnimal( );
animal2.walk( );
animal2.Type( );
}
}
}
{
import flash.display.Sprite;
public class Main extends Sprite
{
public function Main( ):void
{
var animal1:DemosticAnimal=new DemosticAnimal( );
animal1.walk( );
animal1.Type( );
animal1.Trained( );
var animal2:WildAnimal=new WildAnimal( );
animal2.walk( );
animal2.Type( );
}
}
}
Now run the flash document class window and we get the following output.
Animal Created
Parent Animal : is walking on the ground
this is Demostic Animal
Demostic aninmals are Trained to respond to the master's command
Animal Created
Parent Animal : is walking on the ground
this is Wild Animal
Parent Animal : is walking on the ground
this is Demostic Animal
Demostic aninmals are Trained to respond to the master's command
Animal Created
Parent Animal : is walking on the ground
this is Wild Animal
Looking at ouptut , both animal1 and animal2 instances display the superclass constructor message and message in superclass method wailk() , in addition to this displays the custom message by overridding Type() method of superclass and message defined in new addition method Trained().
Inheritance is useful for code resuability with a provision to extend abilities.
Download Source :http://flashsource.in/AS3OOPs1.html