Evolve a basic class in Java

Asked

Viewed 129 times

3

Hello,

I have the following situation, I have an Animal object and I want to evolve it into 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 don’t want to have to instantiate a Puppy and go attribute-to-attribute by filling it with the N data that my Animal has.

Is there any way to make this evolution automatically?

  • 1

    I’m not sure I understand what you want. If you are instantiating one Cachorro in a certain way is instantiating a Animal. In any case you will have to put the data into the object. You can create a routine that does this or you can create values default for members if this is possible (you did this in this short code). In some cases a constructor may be required to start these values default. But the question doesn’t make it very clear what you want.

  • I possess an animal object (brute), because the class that instigated it is unable to specialize it. After treating this Animal I define that it should be a Dog, so I want to specialize it?

  • 1

    I think the "less worse" option would be to create an animal builder who receives an animal as a parameter. So when you convert it to other animals, you would do something like: Cachorro cao = new Cachorro(animal); And regardless of what kind of animal you instantiated you would only need to keep sending the animal(parameter) to the Animal class.

  • Animal meuAnimal = new Cachorro(); but you can only use the methods present in the Animal class

  • @Christianberegula you will answer?

  • You are experiencing a typical problem caused by the use of inheritance by yourself and not to solve a real problem: the promotion of an existing object to a more specialized type object. There is no such thing in practice - a dog is born a dog, it is not born an undefined type of animal to turn into a dog later on. The same is true in modeling a real problem in a real domain - if you notice the need to promote an object to a type below in the hierarchy, it’s because you used inheritance when you shouldn’t have.

Show 1 more comment

2 answers

4


I had actually thought a little differently. I 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 builder with an animal as a parameter.

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

Like all his descendants...

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

And during the execution of the code you would only 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 those values would be the Animal class.

  • I also found this in other questions ( http://stackoverflow.com/questions/4033118/how-to-downcast-java-object ), but it does not escape from this solution. In the case of this link the guy suggests a class of Factory.

1

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

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

  • Example

You got that class Animal:

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 Puppy class should be as follows

public class Cachorro {

  public Cachorro(Animal animal) {

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

  }

}

Thus, when instantiating the objects you will do as follows:

Animal meuAnimal = new Animal();

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

Cachorro cao = new Cachorro(meuAnimal);
  • I think what he wants is to do that but use the special methods of Cachorro

Browser other questions tagged

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