Write a program in java to demonstrate runtime polymorphism or overriding concept or late binding or dynamic binding?

class Animal

{

                public void WhoIAm()

                {

                                System.out.println("I Am Animal");

                }

}

class Dog extends Animal

{

                public void WhoIAm()

                {

                                System.out.println("I Am Dog");

                }

}

class Cat extends Animal

{

                public void WhoIAm()

                {

                                System.out.println("I Am Cat");

                }

}

class Snake extends Animal

{

                public void WhoIAm()

                {

                                System.out.println("I Am Snake");

                }

}

class Main

{

                public static void main(String arg[])

                {

                                Animal a=new Animal(); //Early Binding

                                a.WhoIAm();

                                Animal b=new Dog();     //Late Binding

                                b.WhoIAm();

                                Animal c=new Cat();       //Runtime Binding

                                c.WhoIAm();

                                Animal d=new Snake(); //Dynamic Binding

                                d.WhoIAm();

                }

}

runtime polymorphism or overriding concept or late binding or dynamic binding.PNG

, , , , , , , , , ,

Leave a Reply