Object arrangement by several Classes?

1

I am a beginner in terms of OOP, and right now I am doing a code for a veterinarian where I have to make an arrangement of objects and put animal data.

This is the parent class (which, as you can see is Abstract)

package veterinaria;

public abstract class Animal {

private String nombre;
private int edad;
private int estatura;
private String sexo;

private String color;
private int peso;

Animal () {}

And apart there are 6 classes daughter, Dog, Cat, Reptile, Rodent, Bird and Fish, all these with their exclusive variables and their Getters and Setters.

public class Perro extends Animal {

public Perro () { super(); }

protected byte numPatas;
protected String raza;
protected String vacunas;
protected String pedigree;

My doubt in this case is, if I want to put different animal data in a single arrangement of objects, what could I do in this case?

Before I ask the user to enter what animal you want to enter

    
asked by MirDepressed12 22.04.2018 в 18:07
source

1 answer

1

I hope you're okay.

You can create a list of Animal objects, this is independent of the daughter class to which the object belongs.

public static ArrayList<Animal> animales = new ArrayList<Animal>();

Now, within this list of objects, you will add some of the objects of the child classes you want to create.

Perro kratos = new Perro(numPatas, raza, vacunas, pedigree);
Gato misifu = new Gato(numPatas, raza, vacunas, pedigree);
Reptil floyd = new Reptil(numPatas, raza, vacunas, pedigree);

It should be emphasized, that the properties of each previous animal will vary according to the properties that you have stipulated in your classes, I do not believe that a reptile has a pedigre ...

Then, the most important thing is to add these 3 children objects to our list of Animals, for this we will use the add method

animales.add(kratos);
animales.add(misifu);
animales.add(floyd);

I hope you will be helped, greetings.

    
answered by 22.04.2018 / 18:41
source