POLYMORPHISM:
ploy = many, morphism = phases.
We can store all the objects of extended classes in to variable of parent class. “ OR ”
When we assign the object of parent class to the object of child class then parent class object can call those child class objects which are either inherited or overridden…………
Ad hoc polymorphism.
Pure polymorphism.
PROGRAM 1 :
class Box
{
int w,h;
void info()
{
System.out.println("This is a simple box");
System.out.println("width = "+ w + " hieght "+ h);
}
}
class WoddenBox extends Box
{
int life;
void info( )
{
System.out.println("This is a Wodden box");
}
}
class SteelBox extends Box
{
int wg;
void info( )
{
System.out.println("This is a steel box");
}
}
class LargeWoddenBox extends WoddenBox
{
void info()
{
System.out.println("This is a Huge Wodden box");
}
}
class BoxDemo
{
public static void main ( String ary[ ] )
{
Box x;
Box b1 =new Box( );
WoddenBox wb=new WoddenBox( );
SteelBox s1=new SteelBox( );
LargeWoddenBox p1=new LargeWoddenBox( );
b1.info( );
wb.info( );
s1.info( );
p1.info( );
}
}
Program 2
class Box1
{
int w,h;
void info()
{
System.out.println("This is a simple box");
System.out.println("width = "+ w + " hieght "+ h);
}
}
class WoddenBox extends Box1
{
int life;
void info()
{
System.out.println("This is a Wodden box");
}
}
class SteelBox extends Box1
{
int wg;
void info()
{
System.out.println("This is a steel box");
}
}
class LargeWoddenBox extends WoddenBox
{
void info()
{
System.out.println("This is a Huge Wodden box");
}
}
class BoxDemo
{
public static void main ( String arg[ ] )
{
Box1 b[]=new Box1[5];
b[1]=new Box1();
b[2]=new WoddenBox();
b[3]=new SteelBox();
b[4]=new LargeWoddenBox();
for(int i=1;i<5;i++)
b[i].info();
}