Polymorphically manipulating subsclasses

Asked

Viewed 209 times

2

How Superclass Manipulation of Subclasses Works?

In this code, the array of one 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();
    }

}

}

The other Classes:

Java animal.

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

public abstract void som();

}

Java cow.

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

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

}

Gato.

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

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

Aries.

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

}

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

Does that mean the superclass can receive the subclasses? it is not very clear to me what is happening in the foreach. Polymorphosis is only when several subclasses have different actions?

3 answers

5


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

The foreach is sweeping a array with objects that abstractly possess a lot of Animal. Each of them has its own specific characteristic, each one is a specific animal. So when printing it assumes the behavior of the specific object. It will not call the specific behavior of the Animal, and yes of the Gato, of Vaca, etc., this is polymorphism. Without it, I would try to call the behavior of the Animal, which in this case would not even be allowed to be, rightly, abstract (without real behavior).

Superclass gets nothing, it’s just a template for the other classes, which is just a template to create an object. See What is the difference between a class and an object?.

What’s in the object is what matters during execution. The moment it executes it knows which type is in question and calls the method according to the type, this is solved dynamically. If the method was final, that is, without polymorphism, the declared type would be used and would call the superclass method, which is impossible in this case.

Whether you will have several different actions or not, it doesn’t matter. Polymorphism is a behavior being assumed by the object according to its concrete type, nothing more than this.

  • Did I write something wrong here?

4

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

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

You iterate on an array of animals by calling the methods defined in the Animal class, which is obligatorily superscripted by the Cat, Ram, etc. When, in the iteration, it is recognized by the JVM that that animal is a Cat, shall be called the method superscripted by the Cat. So it will be for the other Animals it contains in the array.

Imagine that you have a collection of cars: Fusion, Azera, Corola, Ferrari Enzo etc. Everyone has the action of opening door, but unlike other cars the Ferrari Enzo opens the door upwards. The action of opening doors is standard for any type of car, but the manufacturer can determine which shape will be opened. When you pass the collection of cars in a foreach and call the open methodPorta() of each car in the collection, it will be accomplished what each manufacturer determined when they overwritten the open methodPorta(). 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

1

Polymorphism, whose concept has already been explained in other responses, can occur between classes that have a "é-um" relation, that is, that have inheritance. In the case of your example, the class Vaca "is a" 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 of its subclasses. Imagine that you need to create a method that mimics the sound of each animal. 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, in short, a bad solution.

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

private void imitarSom(Animal animal);

Ready. Now, you can pass to this one method Vaca, one Gato, one Carneiro and whatever other animal you create in the future, because the method receives a Animal. Remember when I said inheritance is a "is-one" relationship? Well then, any class that "is a" Animal can go as parameter, and the polymorphism will account.

That, then, is what happens in the array and has to do with your doubt:

This means that the superclass can receive the subclasses?

How do you do the for using as argument a superclass and we know that subclasses have relation of "is-one" with it, at runtime the compiler polymorphically accesses the corresponding method of that daughter class being iterated at that exact moment in the 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();
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.