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?