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?
Did I write something wrong here?
– Maniero