Instantiating an object with a different reference

I am learning polymorphism in Java, but I am having some doubts.

public class Animal {

    int numPatas;

    public void fazerBarulho() {

        // Código do Método
    }

    public void comportamento() {

        // Código do Método
    }
}

public class Felino extends Animal {

    public void comportamento() {

        // Aqui o método é reescrito,
        // pois um Felino tem um
        // comportamento próprio
        // de um felino.

    }
}

public class Gato extends Felino {

    // O Gato é um Felino e ao mesmo
    // tempo é um animal.
    // Então ele tem um numero de patas e,
    // o comportamento de um felino.

    public void fazerBarulho() {

        // Mas gato tem um barulho único
        // de um gato.
        // Pois um gato não faz o mesmo
        // barulho de um leão.

    }
}

This concept of polymorphism I understand. But what happens when we do this?

Animal umAnimal  = new Gato();
Author: Maniero, 2014-07-20

2 answers

When you instantiate a cat on the Animal, that animal will have all the properties of the cat as you can see in the example below.

However it will remain an animal and any additional cat or feline method will not be available in it (without casting).

The code below is in gist .

Principal.java

public class Principal {
    public static void main(String[] args) {
        Animal bicho = new Gato();
        System.out.println("Num patas do bicho " + bicho.numPatas);
        bicho.fazerBarulho();
        //bicho.gatoMia(); <- não pode porque não está definido no Animal
        Gato gato = (Gato) bicho;
        gato.gatoMia();
    }
}

Output:

In one animal paws 4

Meow

Meow

Animal.java

public class Animal {
    int numPatas;

    public void fazerBarulho() {

        // Código do Método
    }

    public void comportamento() {

        // Código do Método
    }
}

Feline.java

public class Felino extends Animal{
       public Felino(){
           this.numPatas = 4;
       }
}

Cat.java

public class Gato extends Felino{
    @Override
    public void fazerBarulho(){
        System.out.println("MIAU");
    }
    public void gatoMia(){
        this.fazerBarulho();
    }
}

Note

In this case, the recommended would be that the Animal be an interface as shown below:

public interface Animal {
    public int getNumPatas();
    public void fazerBarulho();
    public void comportamento();
}

And then the other classes would implement such an interface:

public class Felino implements Animal {
    int numPatas;
    public Felino() {
        this.numPatas = 4;
    }

    @Override
    public int getNumPatas() {
        return numPatas;
    }

    @Override
    public void fazerBarulho() {
        throw new UnsupportedOperationException("Não disponível.");
    }

    @Override
    public void comportamento() {
        throw new UnsupportedOperationException("Não disponível.");
    }
}

Note that cat does not need to be modified (with these recommendations and the only change in the main class is bicho.getNumPatas() instead of bicho.numPatas.

 6
Author: Mansueli, 2014-07-20 20:31:10

When you do:

Animal umAnimal  = new Gato();

Means that the animal-type variable umAnimal will behave like a cat.

If you create a method walk in class Animal, and then overwrite that method walk in class Cat, when you call umAnimal.walk (); at runtime will be invoked the method written in class Cat and not what was written in class Animal, for what is in the memory (what was instantiated) is a cat object .

If you want to better and clearly understand the concepts of inheritance, rewriting and polymorphism, I advise you to click here .

 4
Author: electus, 2014-07-20 20:41:32