# Multiple inheritence: combining VerletParticle() with WB\_Point()

**URL:** <https://discourse.processing.org/t/multiple-inheritence-combining-verletparticle-with-wb-point/11921>\
**Category:** Processing.py\
**Created:** [June 8, 2019, 12:19pm UTC](https://discourse.processing.org/t/multiple-inheritence-combining-verletparticle-with-wb-point/11921 "2019-06-08T12:19:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![solub](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/solub/32/333_2.png) [@solub](https://discourse.processing.org/u/solub)\
**Post date:** [June 8, 2019, 12:19pm UTC](https://discourse.processing.org/t/multiple-inheritence-combining-verletparticle-with-wb-point/11921/1 "2019-06-08T12:19:01Z")

</div>

Hi all,

I’m working on a sketch where a `Cell` class needs to inherit both from **Toxiclibs** ’ `VerletParticle()` and **Hemesh** `WB_Point()`. Ideally I would like the Cell object to share specific attributes from the two libraries ([WB\_Point](https://github.com/wblut/HE_Mesh/blob/627640a73826f6f87df466b0b4d1ba937460defb/src/core/wblut/geom/WB_Point.java) connected by [VerletSprings](https://github.com/postspectacular/toxiclibs/blob/master/src.physics/toxi/physics2d/VerletSpring2D.java)).

Is that possible in Python mode ?

With other Processing libraries I used to write something like this:

```auto
class Cell(VerletParticle2D, Locatable):
    def __init__ (self, x, y):
        super(Cell, self). __init__ (x, y)
        self.isActive = False
        
    def getLocation(self):
        return PVector(self.x(), self.y())

```

but this time, `class Cell(VerletParticle2D, WB_Point)` returns the following error:

> processing.app.SketchException: TypeError: Error when calling the metaclass bases no multiple inheritance for Java classes: toxi.physics2d.VerletParticle2D and wblut.geom.WB\_Point  
> at jycessing.mode.run.SketchRunner.convertPythonSketchError(SketchRunner.java:242)  
> at jycessing.mode.run.SketchRunner.lambda$2(SketchRunner.java:119)  
> at java.lang.Thread.run(Thread.java:748)

Is there a way to get around this error ?

---

<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:** [June 8, 2019, 11:19pm UTC](https://discourse.processing.org/t/multiple-inheritence-combining-verletparticle-with-wb-point/11921/2 "2019-06-08T23:19:47Z")

</div>

> [@solub](#):
>
> With other Processing libraries I used to write something like this:
> 
> ```auto
> class Cell(VerletParticle2D, Locatable):
> 
> ```

Which library does Locatable come from? Surely it’s an `interface`, not a Java `class`, right?

> [@solub](#):
>
> **… no multiple inheritance for Java classes:** _toxi.physics2d.VerletParticle2D_ **and** _wblut.geom.WB\_Point_

Java can implement multiple interfaces; but can only extend 1 class!

Jython, just like Python, can inherit from multiple Python classes, creating a mixin among them.

However, when it’s a Java class, Jython can only inherit 1!

Given you can pick 1 only, as a poor workaround, you can make the other Java class become a property of the picked 1:

```auto
class Cell(VerletParticle2D):
    def __init__ (c, *args):
        super(Cell, c). __init__ (*args)
        c.wb_p = WB_Point(*args)
        c.isActive = False

```

In the subclass above, VerletParticle2D was chosen as the parent Java class, while WB\_Point became 1 of its new properties as _wb\_p_.

I’m not sure, but I believe you might want properties _x_ & _y_ to be the same for both VerletParticle2D & WB\_Point, right?

Anyways, I’ve made methods **makeVerletEqualPoint()** & **makePointEqualVerlet()** as another poor workaround:

```auto
add_library('toxiclibs')
add_library('hemesh')

def setup():
    print VerletParticle2D, WB_Point, ENTER

    global cell
    cell = Cell(random(10), random(10))
    print cell, ENTER, cell.wb_p, ENTER

    print cell.wb_p.scaleSelf(random(5))
    print cell.makeVerletEqualPoint(), ENTER

    print cell.scaleSelf(random(5))
    print cell.makePointEqualVerlet()

    exit()

class Cell(VerletParticle2D):
    def __init__ (c, *args, **kws):
        super(Cell, c). __init__ (*args, **kws)
        c.wb_p = WB_Point()
        c.makePointEqualVerlet()
        c.isActive = False

    def makeVerletEqualPoint(c):
        return c.set(c.wb_p.x, c.wb_p.y)

    def makePointEqualVerlet(c):
        c.wb_p.set(c.x(), c.y())
        return c.wb_p

```

---

<div class="post-metadata">

**Author:** ![solub](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/solub/32/333_2.png) [@solub](https://discourse.processing.org/u/solub)\
**Post date:** [June 9, 2019, 5:36pm UTC](https://discourse.processing.org/t/multiple-inheritence-combining-verletparticle-with-wb-point/11921/3 "2019-06-09T17:36:58Z")

</div>

Thank you for the reply and detailed explanations (as always!).

[Locatable](http://www.gicentre.org/utils/reference/org/gicentre/utils/geom/Locatable.html) comes from the [GiCentre](https://www.gicentre.net/utils/hashgrid) library and is indeed an interface, my bad.

I ended-up with the same workaround you’re suggesting and it works “ok” so far. I guess I’ll stick with this solution for now.

Thanks again

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [June 11, 2019, 1:27pm UTC](https://discourse.processing.org/t/multiple-inheritence-combining-verletparticle-with-wb-point/11921/4 "2019-06-11T13:27:00Z")

</div>

The other option is composition. Have your new class simply contain an instance each of the two class objects, and your container class will map their interfaces out as needed / keep them in sync.
