Polymorphically manipulating subsclasses

2

How does subclass manipulation work for the superclass?

In this next code,% w / o of a class, prints the values of other classes.

zoo.java

public class zoo {

public static void main(String[] args) {
    Vaca mimosa = new Vaca();
    Gato bichano = new Gato();
    Carneiro barnabe = new Carneiro();

    Animal bichos[] = {mimosa, bichano, barnabe};

    for(Animal animal : bichos)
    {
            System.out.print(animal.nome + " é da classe " + animal.getClass().getName() + ", tem " + animal.numeroPatas + " patas e faz ");
            animal.som();
            System.out.println();
    }

}

}
  

Other Classes:

Animal.java

public abstract class Animal {
protected String nome;
protected int numeroPatas;

public abstract void som();

}

Cow.java

public class Vaca extends Animal {
public Vaca(){
    this.nome = "Mimosa";
    this.numeroPatas = 4;

}
@Override
public void som(){
    System.out.print("MUUUU");
}

}

Cat.java

public class Gato extends Animal{
public Gato(){
    this.nome = "Bichano";
    this.numeroPatas = 4;

}
@Override
public void som(){
    System.out.print("MIAU");
}
}

Carneiro.java

public class Carneiro extends Animal{
public Carneiro(){
    this.nome = "Banabé";
    this.numeroPatas = 4;

}

@Override
public void som(){
    System.out.print("BÉÉÉ");
}
}

Does this mean that superclass can receive subclasses? it was not very clear to me what is happening in array . Is polymorphism only when several subclasses have different actions?

    
asked by anonymous 06.08.2018 / 21:32

3 answers

1

The polymorphism, whose concept has already been explained in other answers, can occur between classes that have a relation of "is-one", that is, that have inheritance. In the case of your example, class Vaca "is Animal because that extends ( extends ) this. Gato and Carneiro follow the same model.

One of the advantages of inheritance is that you can use a superclass as an argument that serves both this superclass and all its subclasses at the same time. Imagine that you need to create a method that mimics the sound of each bug. Without inheritance and polymorphism, you would have something like this:

private void imitarSom(Carneiro carneiro);
private void imitarSom(Gato gato);
private void imitarSom(Vaca vaca);

For each subclass, a different method, with very similar code, finally a bad solution.

Instead, why not a method that receives a superclass as a parameter?

private void imitarSom(Animal animal);

Ready. You can now pass this method a Vaca , a Gato , a Carneiro and whatever else you create in the future, because the method gets a Animal . Remember when I said that inheritance is a "is-one" relationship? Then, any class that "is a Animal can go as a parameter, and the polymorphism will account."

That's what happens in array and it has to do with your question:

  

Does this mean that superclass can receive subclasses?

Since you do for by using a superclass as an argument and we know that subclasses have a "is-one" relation to it, at runtime the compiler polymorphically accesses the corresponding method of that child class being iterated at that very moment no for , printing the corresponding value.

Animal bichos[] = {mimosa, bichano, barnabe};

for(Animal animal : bichos) {
   System.out.print(animal.nome + " é da classe " + animal.getClass().getName() + ", tem " + animal.numeroPatas + " patas e faz ");
   animal.som();
   System.out.println();
}
    
07.08.2018 / 14:25
5

In general, the superclass should know nothing of the subclasses. And in fact this code does not occur.

foreach is scanning an array with objects that abstractly contain a bunch of Animal . Each one of them has a characteristic of its own, each one is a specific animal. So at the time of printing it assumes the behavior of the specific object. It will not call the specific behavior of Animal , but of Gato , Vaca , etc. this is the polymorphism. Without it, I would try to call the behavior of Animal , which in this case would not even be allowed because it is aptly abstract (no real behavior).

Superclass does not receive anything, it's just a template for other classes, which is just a template for creating an object. See What's the difference between a class and an object? .

What's in the object matters at runtime. At the moment it executes it knows which type is in question and calls the method according to type, this is resolved dynamically. If the method were final , that is, without polymorphism, the declared type would be used and would call the superclass method, which is impossible in this case.

If you have several different actions or not, it is indifferent. Polymorphism is a behavior being assumed by the object according to its concrete type, nothing more than this.

    
06.08.2018 / 21:49
4

Polymorphism is the ability of a method to act in different ways, depending on the object about which it is being called.

When calling a method, the JVM decides which method to invoke depending on the object instantiated in the memory.

You do an iteration on an array of animals by calling the methods defined in the Animal class, which is obligatorily overwritten by the subclasses Cat, Sheep, etc. When, in the iteration, the JVM recognizes that that animal is a Cat, it will be called the method overwritten by the Cat. So it will be for the other Animals that it contains in the array.

Imagine that you have a collection of cars: Fusion, Azera, Corolla, Ferrari Enzo etc. Everyone has the action of opening the door, but unlike the other cars the Ferrari Enzo opens the door up. The action of opening doors is standard for any type of car, however the manufacturer can determine the shape that will open. When you pass the car collection in a foreach and call the openPort () method of each car in the collection, what each manufacturer determined when overwriting the openPort () method is performed. The method invoked is determined by the type of object that is stored in memory.

Animal mimosa = new Vaca(); // Objeto Vaca
Animal bichano = new Gato(); // Objeto Gato
Animal barnabe = new Carneiro(); // Objeto Carneiro
    
07.08.2018 / 00:22