# Problema con Class, extend y herencia

**URL:** https://discourse.processing.org/t/problema-con-class-extend-y-herencia/11204
**Category:** Libraries
**Created:** [May 13, 2019, 6:35pm UTC](https://discourse.processing.org/t/problema-con-class-extend-y-herencia/11204 "2019-05-13T18:35:37Z")
**Posts on this page:** 1
**Showing post:** 4

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [May 14, 2019, 5:29am UTC](https://discourse.processing.org/t/problema-con-class-extend-y-herencia/11204/4 "2019-05-14T05:29:44Z")

</div>

> [@aledante](#):
>
> I thank you for taking the job of translating my question.

I didn’t need to translate anything, nor use Google Translate. 😉

> [@aledante](#):
>
> I would expect to have: Ball, Subclass, Ball, Subclass

Like I had already explained, you’re gonna need to implement **clone()** in your own `class`, rather than relying on the [`new` operator](https://processing.org/reference/new.html):

So instead of:

```auto
Ball hijo() {
  return new Ball();
}

```

Go w/ something like this:

```auto
Ball hijo() {
  return clone();
}

@Override Ball clone() {
  try {
    final Ball hijo = (Ball) super.clone();
    hijo.location = location.get();
    return hijo;
  }
  catch (final CloneNotSupportedException e) {
    throw new RuntimeException(e);
  }
}

```

Notice I had to do: `hijo.location = location.get();`. Here’s the reason why: ⚠

- Your class Ball got 2 fields: _location_ & _nombre_.
- The 1st refers to a PVector, while the 2nd refers to a String.
- Datatype String is immutable. Its content can’t be changed (at least not w/o some hardcore hacking 😈).
- However, datatype PVector is mutable. Its fields _x_, _y_ & _z_ can be freely reassigned.
- If we don’t also clone each mutable field from a class when we call **clone()**, modifying those mutable fields will reflect on both the original object and all of its clones! 😨
- That’s why I invoke the method PVector::**get()** over the cloned field _location_, so the original and the cloned field _location_ won’t share the same PVector object: 🤓
- [ProcessingJS.org/reference/PVector\_get\_/](http://ProcessingJS.org/reference/PVector_get_/)

Below’s my new attempt sketch “Cloneable Locatable”. Any doubts about it just ask: 😇

```auto
/**
 * Cloneable Locatable (v1.0.1)
 * GoToLoop (2019/May/14)
 * Discourse.Processing.org/t/problema-con-class-extend-y-herencia/11204/4
 */

import org.gicentre.utils.geom.HashGrid;
import org.gicentre.utils.geom.Locatable;

import java.util.Collection;
//import java.util.Set;

static final int SIZE = 10;
final Collection<Bola> temp = new ArrayList<Bola>();

Collection<Bola> pelotas;
//Set<Bola> pelotas;
//HashGrid<Bola> pelotas;

void setup() {
  size(400, 400);
  noLoop();

  pelotas = new HashGrid<Bola>(width, height, SIZE);

  pelotas.add(new Bola());
  pelotas.add(new Bolita());
}

void draw() {
  background((color) random(#000000));
  getSurface().setTitle("Frame: " + frameCount);
  println("\nPelotas: " + pelotas.size());
  for (final Bola b : pelotas) println(b);
}

void mousePressed() {
  temp.clear();
  for (final Bola b : pelotas) temp.add(b.hijo());
  pelotas.addAll(temp);
  redraw = true;
}

class Bola implements Locatable, Cloneable {
  PVector location = new PVector(40, 20);
  String nombre = getClass().getSimpleName();

  Bola hijo() {
    return clone();
  }

  @Override Bola clone() {
    try {
      final Bola pelota = (Bola) super.clone();
      pelota.location = location.get();
      return pelota;
    }
    catch (final CloneNotSupportedException e) {
      throw new RuntimeException(e);
    }
  }

  @Override PVector getLocation() {
    return location;
  }

  @Override String toString() {
    return nombre + ": " + location;
  }
}

class Bolita extends Bola {
}

```

---

_[View the full topic](https://discourse.processing.org/t/problema-con-class-extend-y-herencia/11204)._
