This seems to be a POO exercise on inheritance, not to create one class within another, but to extend the child classes with the mother class. I implemented a version of my own here based on your descriptions.
This is the base class, I left all attributes as String for ease.
Java animal.
public class Animal {
protected String nome, comprimento, patas, cor, ambiente, velocidade, caracteristica, alimentoPreferido;
public Animal(String nome) {
this.nome = nome;
this.comprimento = "0m";
this.patas = "0";
this.cor = "Nenhuma";
this.ambiente = "Nenhum";
this.velocidade = "0m/s";
this.caracteristica = "Nenhuma";
this.alimentoPreferido = "Nenhum";
}
public String getNome() { return nome; }
public void setNome(String nome) { this.nome = nome; }
public String getComprimento() { return comprimento; }
public void setComprimento(String comprimento) { this.comprimento = comprimento; }
public String getPatas() { return patas; }
public void setPatas(String patas) { this.patas = patas; }
public String getCor() { return cor; }
public void setCor(String cor) { this.cor = cor; }
public String getAmbiente() { return ambiente; }
public void setAmbiente(String ambiente) { this.ambiente = ambiente; }
public String getVelocidade() { return velocidade; }
public void setVelocidade(String velocidade) { this.velocidade = velocidade; }
public String getCaracteristica() { return caracteristica; }
public void setCaracteristica(String caracteristica) { this.caracteristica = caracteristica; }
public String getAlimentoPreferido() { return alimentoPreferido; }
public void setAlimentoPreferido(String alimentoPreferido) { this.alimentoPreferido = alimentoPreferido; }
}
Subclass that extends animal inheriting its attributes and methods.
Java fish.
public class Peixe extends Animal {
public Peixe(String nome) {
super(nome);
this.comprimento = "15cm";
this.patas = "0";
this.cor = "Cinzenta";
this.ambiente = "Mar";
this.velocidade = "2m/s";
this.caracteristica = "Barbatanas e Cauda";
this.alimentoPreferido = "Algas";
}
}
Subclass that extends animal inheriting its attributes and methods.
Mamifero.java.
public class Mamifero extends Animal {
public Mamifero(String nome) {
super(nome);
this.ambiente = "Terra";
this.caracteristica = "Pelo em todo ou em parte do corpo";
}
}
Subclass that extends Mamifero inheriting his attributes and methods.
Java bear.
public class Urso extends Mamifero {
public Urso(String nome) {
super(nome);
this.comprimento = "1m";
this.patas = "4";
this.cor = "Castanho";
this.velocidade = "1m/s";
this.caracteristica = "Pelo em todo corpo";
this.alimentoPreferido = "Mel";
}
}
And a class to test Main.java
public class Main {
public static void main(String[] args) {
Animal animal = new Animal("Animal Base");
Peixe peixe = new Peixe("Nemo");
Mamifero mamifero = new Mamifero("Mamifero Base");
Urso urso = new Urso("Zé Colmeia");
System.out.println(animal.getNome());
System.out.println(peixe.getPatas());
System.out.println(mamifero.getCaracteristica());
System.out.println(urso.getAlimentoPreferido());
}
}
It is an exercise about inheritance. Fish and mammals derive from animal and bear derived from mammal which in turn is derived from animal.
– Augusto Vasques
From what I understand, this is an exercise in inheritance. Yes, they are independent classes, but some classes inherit attributes and methods from other classes. Fish inherits from animal class, mammal also inherits from animal class and bear inherits from mammal
– Juliana Marques