3

I have an abstract class Specie, and then class Animals which extends species, and then my classes for animals (e.g. Sheep). In Animals I have a method which checks if two objects are on the same position on the map, and if the are the same type (e.g. Sheep and Sheep).

If yes it creates another Sheep. And I have problem with that I tried something like that

Specie new_specie = this;

And I have a copy consturctor in my Sheep class

     public Sheep(Sheep new_sheep){

     this(new_sheep.get_x(),new_sheep.get_y(), new_sheep.get_img());
}

And then save it to the array which contains all the objects

species[speciesAmount] = new_specie;

But it just saves the same object in two elements of an array

species[0]
species[2]

same object. Any ideas?

But there is another problem, I'll have more types of animals(eq. Wolf) and I can't do

new Specie(this)

because it's an abstract class.

How to make it to call the proper constructor?

edit. I solved it, I used clone() method.

6
  • 2
    public Sheep(Sheep new_sheep){ Do you see the problem with this? basically, you'll need to have a Sheep instance, in order to be able to create one ... Commented May 11, 2015 at 11:16
  • But there is another problem, I'll have more types of animals(eq. Wolf) and I can't do new Specie(this) because it's an abstract class, how to make it to call proper consturcotr? Commented May 11, 2015 at 11:24
  • 1
    new Specie(this) is a bit nonsense imho, just use the clone method if you need something like that. Commented May 11, 2015 at 11:29
  • I heard that I shouldn't use it because it's outdate or something like that . but I'll give it a try thanks. Commented May 11, 2015 at 11:31
  • I wrote an answer that should help you with this cloning. You should however learn a bit more about Java, you are bringing your C++ habits to Java programming (including naming conventions) and of course, it has its downsides. Commented May 11, 2015 at 11:58

3 Answers 3

3

Specie new_specie = this; will just create a new reference called new_specie and let it refer to the same object as this. Hence species[speciesAmount] = new_specie; will assign the same reference to the array entry (which is itself a reference).

Your code doesn't call the copy constructor at all. Unlike C++ in Java calling a copy constructor has to look like this:

Specie new_specie = new Sheep(this);

Btw, you might want to have a look into the Clonable interface which is meant to provide for object copying.

Update: Regarding your second question the solution depends on what you want to achieve. If you just want a copy of the concrete species that is on a field whenever there are two of them, you could just compare the classes of both species (using the getClass() method) or check equality using equals() (depends on how you define that, it's often more than equal classes).

Then you basically just call Species new_species = species_on_field.clone() and get a clone of what was on that field before.

Another option might be to use the factory pattern or a prototype based on the class of the current species on that field. I'll leave those for you to look up on the net though.

Sign up to request clarification or add additional context in comments.

Comments

2

In Java, you can't call a copy constructur with an assignment. You have to call it!

species[0] = new Sheep(this);

Comments

1

You can compare species by comparing their class, like:

if (specieA.getClass() == specieB.getClass) {

Then, your next step is creating a new specie. In runtime, you can access this information using reflections. It is not really something beautiful, but sometimes it cannot be avoided (e.g., you don't want to create a switch-case for every specie):

    Class<?> clazz = specieA.getClass();
    Specie newSpecie = clazz.getConstructor(clazz).newInstance(specieA);
}

This looks for a constructor of specieA's class (named clazz here) that accepts an instance of clazz as its input parameter. This way you should be able to clone anything (provided that it has a copy constructor, of course). If there is none, an exception will be thrown (you'll need a try-catch).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.