How to create this class hierarchy?

Asked

Viewed 487 times

2

In this exercise, I have to create a class within another class or are independent classes?

  • An animal contains a name, length, number of legs (default is 4), one color, environment and a speed (in m/s).
  • A fish is an animal, has 0 legs, its environment is the sea (standard), grey (standard). In addition, the fish has as characteristic: fins and tail;
  • A mammal is an animal, its standard environment is the earth and has as characteristic: hair in all or in part of the body;
  • A bear is a mammal, brown in color and its preferred food is honey and hair throughout body.

Code animal, fish, mammal and bear classes considering they all have a builder named.

  • It is an exercise about inheritance. Fish and mammals derive from animal and bear derived from mammal which in turn is derived from animal.

  • 1

    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

1 answer

1


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());
    }
}

Browser other questions tagged

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