Evolve a basic class in Java

3

Hello,

I have the following situation, I have an Animal object and I want to develop it for a Puppy:

    public class Animal {
        private boolean alive = true;
        public boolean isAlive() {
            return alive;
        }
        public void setAlive(boolean alive) {
            this.alive = alive;
        }
    }
    public abstract class Mamifero extends Animal {
        public abstract void mamar();
    }
    public class Cachorro extends Mamifero {
        @Override
        public void mamar() {
            System.out.println("Done");
        }
    }

I know this (below) does not work:

Animal meuAnimal = new Animal();
Cachorro cao = (Cachorro) meuAnimal;

But I do not want to have to instantiate a Dog and go attribute-to-attribute by filling it with the N data my Animal has.

Is there any way to make this evolution automatically?

    
asked by anonymous 01.10.2015 / 21:16

2 answers

4

I had actually thought a little differently. It would look something like this:

    public class Animal {
        private boolean alive = true;
        public Animal() {
        }
        public Animal(Animal animal) {
            this.alive = animal.alive;
        }
        public boolean isAlive() {
            return alive;
        }
        public void setAlive(boolean alive) {
            this.alive = alive;
        }
    }

The animal class would have a constructor with an animal as a parameter.

    public abstract class Mamifero extends Animal {
        public Mamifero() {
        }
        public Mamifero(Animal animal) {
            super(animal);
        }
        public abstract void mamar();
    }

Just like all your descendants ...

    public class Cachorro extends Mamifero {
        public Cachorro() {
        }
        public Cachorro(Animal animal) {
            super(animal);
        }
        @Override
        public void mamar() {
            System.out.println("Done");
        }
    }

And while running the code you just need to instantiate your new type of animal.

Cachorro cao = new Cachorro(animal);
Gato gato = new Gato(animal);
Vaca vaca = new Vaca(animal);

And the only class that would care to redistribute these values would be the Animal class.

    
01.10.2015 / 22:26
1

"Is there any way I can instantiate a Dog based on an existing Animal object?"

Do the following: Create a constructor in the Dog class that receives an Animal as a parameter and passes the values of that Animal object to that Puppy class.

  • Example

You have this Animal class:

public class Animal {

  int atributo1;
  double atributo2;
  String atributo3;

  public void setAtributo1(int atributo) {
    atributo1 = atributo;
  }

  public int getAtributo1() {
    return this.atributo1;
  }

  public void setAtributo2(double atributo) {
    atributo2 = atributo;
  }

  public void getAtributo2() {
    return this.atributo2;
  }

  public void setAtributo3(String atributo) {
    atributo3 = atributo;
  }

  public void getAtributo3() {
    return this.atributo3;
  }

}

Your Dog class should be as follows

public class Cachorro {

  public Cachorro(Animal animal) {

    this.setAtributo1(animal.getAtributo1());   
    this.setAtributo2(animal.getAtributo2());
    this.setAtributo3(animal.getAtributo3());

  }

}

In this way, when instantiating the objects you will do the following:

Animal meuAnimal = new Animal();

meuAnimal.setAtributo1(/*VALOR*/);
meuAnimal.setAtributo2(/*VALOR*/);
meuAnimal.setAtributo3(/*VALOR*/);

Cachorro cao = new Cachorro(meuAnimal);
    
01.10.2015 / 22:08