# The class Object

**URL:** https://discourse.processing.org/t/the-class-object/19693
**Category:** Coding Questions
**Created:** [April 12, 2020, 12:28pm UTC](https://discourse.processing.org/t/the-class-object/19693 "2020-04-12T12:28:14Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![Baz](https://avatars.discourse-cdn.com/v4/letter/b/c4cdca/32.png) [@Baz](https://discourse.processing.org/u/Baz)
#### Post date: [April 12, 2020, 12:28pm UTC](https://discourse.processing.org/t/the-class-object/19693/1 "2020-04-12T12:28:14Z")

</div>

I’m trying to write a simple 2D physics simulator. In it, I have 3 classes : One for box, one for circle, and one called manifold which serve for collision detection.  
Long story short, the manifold class need to have 2 variable of the thing that colide inside that can either be a box or circle. I found out that instead of having 2 variable box and 2 variable circle and not declare some of them (which would be increadibly messy) I could have 2 variable of the type Object, that can be one or the other without any problem.

The thing is, when I try to access some value/field inside of those 2 Object, for exemple var1.pos, I get an error saying that ‘the global variable “pos” does not exist’.

How could I still access the values inside of those Object variable, and how should I use these to be as the class they actually are ?

---

<div class="post-metadata">

### Author: ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)
#### Post date: [April 12, 2020, 3:06pm UTC](https://discourse.processing.org/t/the-class-object/19693/2 "2020-04-12T15:06:50Z")

</div>

I suspect you are doing it right, but are having trouble because there’s already an “Object” class defined that’s hidden away via Processing’s magic which is having a name collision wth the Object class you are defining.

If you rename your “Object” class to “MyObject”, in all the places you use it, do you still have the same problem?

---

<div class="post-metadata">

### Author: ![Baz](https://avatars.discourse-cdn.com/v4/letter/b/c4cdca/32.png) [@Baz](https://discourse.processing.org/u/Baz)
#### Post date: [April 12, 2020, 3:26pm UTC](https://discourse.processing.org/t/the-class-object/19693/3 "2020-04-12T15:26:13Z")

</div>

I have created no “Object” class, i’m just using the [native Java Object class](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Object.html) wich is the root of all class / type of variable, so you can more or less put everything here no matter the type/class of the variable/Object. I guess that I’m doing this wrong or that I don’t understand how you’re supposed to use it.

---

<div class="post-metadata">

### Author: ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)
#### Post date: [April 12, 2020, 3:37pm UTC](https://discourse.processing.org/t/the-class-object/19693/4 "2020-04-12T15:37:12Z")

</div>

Without seeing the (possible sample/example) code of your problem, it’s hard to say what is wrong.

The native Java Object class doesn’t have a “pos” attribute! How/why do you think one is added/defined?

Also consider:

```auto
class MyShape {
  boolean is_circle;
  float x, y;
  MyShape(){
    x = random(width);
    y = random(height);
    is_circle = random(1) < .5;
  }
  void render(){
    if( is_circle ){
      ellipse(x,y,20,20);
    } else {
      rect(x-10,y-10,20,20);
    }
  }
}

```

---

<div class="post-metadata">

### Author: ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)
#### Post date: [April 12, 2020, 3:39pm UTC](https://discourse.processing.org/t/the-class-object/19693/5 "2020-04-12T15:39:11Z")

</div>

```auto
Object a = new manifold();
Object b = new box();
Object c = new circle();

```

Are all valid statements but what data type are they?

In fact they are all of type `Object` so when you do something like

```auto
a.checkCollision(b, c);

```

It won’t work because `checkCollision` is a method inside the `mmanifold` class and does not exist.

```auto
((manifold)a).checkCollision(b, c);

```

will work because you have cast the variable to the correct class.

The same applies to variable `a` and `c` they can only access the methods available in the `java.lang.Object` class.

---

<div class="post-metadata">

### Author: ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)
#### Post date: [April 12, 2020, 3:42pm UTC](https://discourse.processing.org/t/the-class-object/19693/6 "2020-04-12T15:42:20Z")

</div>

The solution is to either use inheritance or interfaces so that objects of type `circle` and `box` look the same to the `manifold` class but have different implementations for common methods.

---

<div class="post-metadata">

### Author: ![Baz](https://avatars.discourse-cdn.com/v4/letter/b/c4cdca/32.png) [@Baz](https://discourse.processing.org/u/Baz)
#### Post date: [April 12, 2020, 4:32pm UTC](https://discourse.processing.org/t/the-class-object/19693/7 "2020-04-12T16:32:45Z")

</div>

well because I did something like  
Object a = new box()  
and because box was defined as having a “pos” attribute. But I guess that don’t really make sens.  
I’ll probably do the option of combining them under one class as you’ve shown, but i’ll avoid doing that if I can fine an other way around keeping them as separate classes.  
(I don’t know how to indent the code in my post, sorry)

---

<div class="post-metadata">

### Author: ![Baz](https://avatars.discourse-cdn.com/v4/letter/b/c4cdca/32.png) [@Baz](https://discourse.processing.org/u/Baz)
#### Post date: [April 12, 2020, 4:34pm UTC](https://discourse.processing.org/t/the-class-object/19693/8 "2020-04-12T16:34:05Z")

</div>

Oh yeah this totally work and is still pretty clean ! thanks a lot !

---

<div class="post-metadata">

### Author: ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)
#### Post date: [April 12, 2020, 5:00pm UTC](https://discourse.processing.org/t/the-class-object/19693/9 "2020-04-12T17:00:10Z")

</div>

Not sure what you got to work but this minimal sketch shows how it can be done with inheritance. If you run the sketch you will see that the manifold method will accept 2 objects of type Shapes2D but inside the method we can call methods in the actual class.

```auto
public void setup() {
  Manifold m = new Manifold();
  Circle c = new Circle(60, 80, 25);
  Box b = new Box(20,40, 115, 55);
  m.haveCollided(b, c);
}

class Manifold {

  boolean haveCollided(Shape2D s0, Shape2D s1) {
    s0.desc();
    s1.desc();
    return false;
  }
}

abstract class Shape2D {
  float x;
  float y;

  Shape2D(float x, float y) {
    this.x = x;
    this.y = y;
  }

  abstract void desc();
}

class Box extends Shape2D {

  float w;
  float h;
  Box(float x, float y, float w, float h) {
    super(x, y);
    this.w = w;
    this.h = h;
  }

  void desc() {
    println("Box @", x, y, "size=", w, h);
  }
}

class Circle extends Shape2D {

  float r;

  Circle(float x, float y, float r) {
    super(x, y);
    this.r = r;
  }

  void desc() {
    println("Circle @ ", x, y, "radius = ", r);
  }
}

```

---

<div class="post-metadata">

### Author: ![Baz](https://avatars.discourse-cdn.com/v4/letter/b/c4cdca/32.png) [@Baz](https://discourse.processing.org/u/Baz)
#### Post date: [April 12, 2020, 5:14pm UTC](https://discourse.processing.org/t/the-class-object/19693/10 "2020-04-12T17:14:33Z")

</div>

This could be a good method but for the same reason that I don’t want to fusion the two classes, i think i’ll stay with the casting on an Object variable plus the use of `if( myShapeVar instanceof Box)` to determine the actual class of the shape. I’m not really used to the how class work, particularly the inheritance, but i’ll think about it if I get stuck again over a problem like that, thanks !

---

<div class="post-metadata">

### Author: ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)
#### Post date: [April 12, 2020, 5:19pm UTC](https://discourse.processing.org/t/the-class-object/19693/11 "2020-04-12T17:19:07Z")

</div>

That’s fine, do what you are comfortable with.

I do recommend that you learn the object orientated paradigm at some time it makes similar projects so much easier to create, edit and maintain.
