Tuesday, December 27, 2011

Java program that illustrates runtime polymorphism


// AIM : Java program that illustrates runtime polymorphism

import java.io.*;

abstract class animal
{
        //abstract method
        public abstract void move();
}
class man extends animal
{
        public void move()
        {
                System.out.println("Walks on his two legs..");
        }
}
class horse extends man
{
        public void move()
        {
                System.out.println("Runs on it's 4 legs..");
        }
}
class crocodile extends horse
{
        public void move()
        {
                System.out.println("Crawls on its belly..");
        }
}



class  poly
{
        public static void main(String[] args)
{
                System.out.print("\nRuntime polymorphism\n\n");
                animal a;
                a=new man();
                a.move(); //behaviour changed as man
                a=new crocodile();
                a.move(); //behaviour changed as crocodile
                a=new horse();
                a.move(); //behaviour changed as horse
}
}




No comments:

Post a Comment