# 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:** 2

<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 13, 2019, 9:17pm UTC](https://discourse.processing.org/t/problema-con-class-extend-y-herencia/11204/2 "2019-05-13T21:17:03Z")

</div>

Disclaimer: I know English & Portuguese only. 😑

And although I can understand Spanish more or less, still I can’t properly reply using it, sorry. 😞

Anyways, we can always count on Google Translate, right? 😌

> **[Google Translate](https://translate.google.com/#view=home&op=translate&sl=en&tl=es)**
>
> Google's service, offered free of charge, instantly translates words, phrases, and web pages between English and over 100 other languages.

> [@aledante](#):
>
> Cuando llamo al procedimiento **hijo()** de la clase Perro me crea otro Animal y no un Perro.

Indeed, if your method Animal::**hijo()** is something like this:

```auto
class Animal {
  Animal hijo(final String name) {
    return new Animal(name);
  }
}

```

And you’ve got a `class` that `extends` it:

```auto
class Perro extends Animal {
  Perro(final String name) {
    super(name);
  }
}

```

Java won’t “automagically” upgrade the datatype Animal in `return new Animal(name);` to the datatype of that subclass like `return new Perro(name);` in any way! 😢

As a workaround, we could use Java reflection techniques in order to always instantiate the current subclass’ Constructor: 🏗  
[Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/lang/reflect/Constructor.html](http://Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/lang/reflect/Constructor.html)

However, reflection is complicated! And thus it should be used only if there’s no other alternative: ⚠

> [@\[SOLVED\] Instantiating a particular class constructor using reflection](https://discourse.processing.org/t/instantiating-a-particular-class-constructor-using-reflection/7968/9):
>
> The (cast\_type) operator gotta be of a datatype compatible w/ the class you’re attempting to instantiate w/ Constructor::newInstance(). face_with_monocle BtW, here’s version 3.1. It’s now using Class.forName() in place of Class::getDeclaredClasses(): innocent /\*\* \* Reflexive Inner Class Instantiation (v3.1) \* GoToLoop (2019/Jan/30) \* \* https://Discourse.Processing.org/t/ \* instantiating-a-particular-class-constructor-using-reflection/7968/9 \* \* https://Forum.Processing.org/two/disc…

And a simpler alternative is to implement the Cloneable `interface` and its **clone()** method: 💡  
[Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Cloneable.html](http://Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Cloneable.html)  
[Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Object.html#clone()](http://Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Object.html#clone())

Here’s an example of a `class` named Pair which `implements` Cloneable: 🤓

> [@Functions argument](https://discourse.processing.org/t/functions-argument/11010/5):
>
> Or even better, use it as an actual full-fledged class, w/ constructors, fields & methods: triumph // https://Discourse.Processing.org/t/functions-argument/11010/5 // GoToLoop (v1.1) (2019/May/09) final Pair pair = new Pair(5, -10); void setup() { println(pair); // [5, -10] println(pair.calc('y')); // [-50, 540] println(pair.set(5, -10)); // [5, -10] println(pair.calc('u')); // [3, -2] println(pair.clone()); // [3, -2] exit(); } static final class…

And inspired by my previous sketch above, here’s “Cloneable Inheritance”: 🤠

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

import java.util.List;
final List<Animal> animales = new ArrayList<Animal>();

void setup() {
  animales.add(new Animal("Madre"));
  println(animales);

  animales.add(animales.get(animales.size() - 1).hijo("Hijo"));
  println(animales);

  animales.add(new Perro("Rex"));
  println(animales);

  animales.add(animales.get(animales.size() - 1).hijo("Perla"));
  println(animales);

  exit();
}

class Animal implements Cloneable {
  String name;

  Animal() {
  }

  Animal(final String name) {
    set(name);
  }

  Animal set(final String nick) {
    name = nick;
    return this;
  }

  Animal hijo(final String name) {
    //return new Animal(name);
    return clone().set(name);
  }

  @Override Animal clone() {
    try {
      return (Animal) super.clone();
    }
    catch (final CloneNotSupportedException e) {
      throw new RuntimeException(e);
    }
  }

  @Override String toString() {
    return name + " el " + getClass().getSimpleName();
  }
}

class Perro extends Animal {
  Perro() {
  }

  Perro(final String name) {
    super(name);
  }
}

```

---

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