At 12:05 PM 5/30/2002 +0530, you wrote:
>Hello all,
> 
>     Could any body of you explain me with an example what is meant by 
>Polymorphism.....I went through many sites,,but it is bit confusing and not clear...
>
>Thanks and regards,
>
>Santosh 



take a look at this:


public class
Animal {
        public void makeNoise() {
                System.out.println("what am I supposed to say?");
        }
}

public class
Dog extends Animal {
        public void makeNoise() {
                System.out.println("woof");
        }
}

public class
Cat extends Animal {
        public void makeNoise() {
                System.out.println("meow");
        }
}

public class
AnimalTest() {
        public static void
        main(String[]  args) {
                Animal
                        a1 = new Dog(),
                        a2 = new Cat();
                a1.makeNoise();
                a2.makeNoise();
        }
}


The output, obviously is:

woof
meow

This is polymorphism in action.

You have 2 Animal references (a1 & a2).  But the behavior of these objects depends not 
on the declared type of the references (Animal), but upon the actual type of the 
objects (Dog and Cat).

That's polymorphism.  So in a nutshell, polymorphism basically means that objects of 
the same interface/type exhibit different behavior depending on their actual subclass 
type.

HTH.


DR


To change your membership options, refer to:
http://www.sys-con.com/java/list.cfm

Reply via email to