Monday 8 June 2015

Runtime Polymorphism

Runtime Polymorphism:
class shape
{
area();
}
class square extends shape
{
area();
}
class circle extends shape
{
area();
}
class rectangle extends shape
{
area();
}
main()
{
shape S=new shape();
S.O.P.(2 for circle, 1 for square, 3 for rectangle);
switch()
{
case 1: square s=new square();
S=s;
case 2: circle c=new circle();
S=c;
case 3: rectangle r=new rectangle();
S=r;
}
S.area(); //what area() will be called depends on user input at run time.
}

No comments:

Post a Comment