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>
![enter image description here](https://i.stack.imgur.com/2GhXY.png)

All of these class definitions:

```
class Dog extends Animals
{}
class Cat extends Animals
{}
class Wolf extends Animals
{}

```

define new classes that are subclasses of the `Animals` class.

> 
> The dictionary definition of polymorphism refers to a principle in biology in which an organism or species can have many different forms or stages. This principle can also be applied to object-oriented programming and languages like the Java language. Subclasses of a class can define their own unique behaviors and yet share some of the same functionality of the parent class.
> 
> 
> 

As illustrated in the picture above, subclasses inherit some features of the base class and inside them you have access to both the subclass and subclass features(that are visible to subclass).

**Some advantages of polymorphism**: 

* Same interface could be used for creating methods with different implementations
* Reduces the volume of work in terms of distinguishing and handling various objects
* Supports building extensible systems
* Complete implementation can be replaced by using same method signatures