Question: i can't understand the following program, how to achieve polymorphism in this coding, please explain in simple way...because i am not strong knowledge in java...

```
 class Animal
   {
       eat()       //here this class has one method

    }
 class Dog extends Animals
    {}
 class Cat extends Animals
    {}
 class Wolf extends Animals
    {}
 class MyMain
    {
       public static void main(String args[])
       {
       Animals[] A=new Animals[3];
       A[0]=new Dog();         //parent object reference is used to refer child object
       A[1]=new Cat();         
       A[2]=new Wolf();
       for(int i=0;i<3;i++)
         A[i].eat();
       }

```

please anybody explain this  

which book i have to study for improve knowledge in java?

Answer: 
**Polymorphism in simple terms**

Polymorphism means "Many forms or shapes", which is a *phenomenon* where objects behave differently although they are closely *related*. In the following example we have Cat, Dog and Wolf which are all Animal therefore are related but they perform "makesound" differently. In Java the fact that they are related is denoted by Inheritance, the behavior is denoted by a Method and this phenomenon is called Polymorphism. 

**Polymorphism using your example**

I would declare makesound() instead of eat() since it explains it clearly:

```
 class Animal
   {
     // This method will be overridden by Dog, Wolf and Cat classes.
     makesound(){
              System.out.println("make sound");  }
  }

 class Dog extends Animals
    {
     makesound()
              {System.out.println("Bau");}
    }

class Cat extends Animals
    {
     makesound(){System.out.println("Miao");}
    }

class Wolf extends Animals
   {
     makesound(){System.out.println("Uuuu");}
   }

class MyMain
  {
     public static void main(String args[])
     {
      /* This is where polymorphism happens, although A[0],A[1],A[2] are all Animals, 
         at run-time we realize that A[0] is infact dog, A[1] is cat and A[2] is wolf
         so when call makesound on these objects, the method behaves differently so the
         name polymorphism.*/ 

     Animals[] A=new Animals[3];
     A[0]=new Dog();         
     A[1]=new Cat();         
     A[2]=new Wolf();
     for(int i=0;i<3;i++)
       A[i].makesound();
     }

```

On a sidenote before you try to understand what polymorphism, please read what method overriding is: <http://docs.oracle.com/javase/tutorial/java/IandI/override.html>
Polymorphism can be explained better if you use an interface (because it can not be instantiated):

```
interface Animal {
    void makeSomeNoise();
}

class Cow implements Animal {
    public void makeSomeNoise() {
        System.out.println("Moo!");
    }
}

class Fox implements Animal {
    public void makeSomeNoise() {
        System.out.println("Ring-ding-ding-ding-dingeringeding!");
    }
}

```

This shows that an animal can not make any noise, but a cow and a fox can. But they make different noises. This is the "principle in biology in which an organism or species can have many different forms or stages". Basically one thing can many different faces.

If you now declare your array like:

```
Animal[] animals = new Animal[2];
animals[0] = new Cow();
animals[1] = new Fox();

```

And then let them make some noise with:

```
animals[0].makeSomeNoise();
animals[1].makeSomeNoise();

```

You would get following output:

```
Moo!
Ring-ding-ding-ding-dingeringeding!

```

So you have two animals but they make different noises.