# Trying to convert 2 objects into a super class and a subclass

**URL:** <https://discourse.processing.org/t/trying-to-convert-2-objects-into-a-super-class-and-a-subclass/19977>\
**Category:** Coding Questions\
**Created:** [April 20, 2020, 10:26pm UTC](https://discourse.processing.org/t/trying-to-convert-2-objects-into-a-super-class-and-a-subclass/19977 "2020-04-20T22:26:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![debxyz](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@debxyz](https://discourse.processing.org/u/debxyz)\
**Post date:** [April 20, 2020, 10:26pm UTC](https://discourse.processing.org/t/trying-to-convert-2-objects-into-a-super-class-and-a-subclass/19977/1 "2020-04-20T22:26:55Z")

</div>

I’ve watched the videos, read the reference, but am hitting a wall. Not sure what I’m overlooking at this point.

Attempting to convert an earlier sketch into one implementing inheritance classes.

I _think_ part of the problem are the functions: **void rectAddColor()** and **void arcAddColor()** in the super and subclasses.

Including 2 sketches:  
**The FIRST** is the problem sketch.  
**The SECOND** is the original sketch for reference.  
I’m trying to have the FIRST and SECOND sketches **look the same** when run.

Any guidance / hints most welcome!  
Thank you!  
🤓

**FIRST SKETCH here is problematic (doesn’t run):**

```auto
// halfmoon motif from SUPER-CLASS & SUB-CLASS
// --------------------------------------- Main
Halfmoon moonRect;
Halfcrescent moonArc;

void setup() {
  size(200, 200);

  moonRect = new Halfmoon(0, 0, 200);
  moonArc = new Halfcrescent(0, 0, 200);
  
  frameRate(1);
}

void draw() {
background (255);

  moonRect.display();
  moonArc.display();
}
// --------------------------------- Super Class

class Halfmoon {

  color c1 = #FFFFFF;
  color c2 = #BEBEBE;
  color c3 = #7F7F7F;
  color c4 = #3F3F3F;
  color c5 = #000000;

  color [] grays = {c1, c2, c3, c4, c5};

  int x, y, sz;

  Halfmoon (int tempX, int tempY, int tempSz) {
    x = tempX;
    y = tempY;
    sz = tempSz;
  }

  void display() {
    noStroke();
    translate (sz/2, sz/2);
    rectAddColor (grays[int(random(grays.length))]);
  }

  void rectAddColor (color rectColor) {

    fill (rectColor);
    rectMode (CENTER);
    rect (x, y, sz, sz);
  }
}
// -------------------------------------------- subClass

class Halfcrescent extends Halfmoon {

  float deg = 90;
  float rad = radians (deg);
  
  //int x;
  //int y;
  //int sz;

  Halfcrescent (int x, int y, int sz) {
    super (x, y, sz);
  }

  void display() {

    noStroke();
    float r = random(1);
    println (r);

    if (r < 0.40) {
      rotate (rad);
    } else if (r > 0.40 && r < 0.50) {
      rotate (rad*3);
    } else if (r > 0.50 && r < 0.65) {
      rotate (rad*2);
    } else {
      rotate (rad*4);
    }

    arcAddColor (grays[int(random(grays.length))]);
  }
}
void arcAddColor (color arcColor) {
  
  //int x = 0; int y = 0; int sz = 0;
  
  fill (arcColor);
  arc (x, y, sz, sz, 0, PI);
}

```

**SECOND SKETCH for reference (trying to make FIRST sketch look like this):**

```auto
// halfmoon motif from 2 SEPARATE OBJECTS

HalfmoonRect hlfMn_R;
HalfmoonArc hlfMn_A;

void setup() {
  size(200, 200);

  hlfMn_R = new HalfmoonRect();
  hlfMn_A = new HalfmoonArc();
  
  frameRate(1);
}

void draw() {

  background (255);

  hlfMn_R.display();
  hlfMn_A.display();

}
class HalfmoonArc {

  color c1 = #FFFFFF;
  color c2 = #BEBEBE;
  color c3 = #7F7F7F;
  color c4 = #3F3F3F;
  color c5 = #000000;

  color [] grays = {c1, c2, c3, c4, c5};

  float deg = 90; // 90 degrees
  float rad = radians(deg); // convert to radians

  //-------------------------------------
void display() {

    noStroke();

    float r = random(1);
    println (r);

    if (r < 0.40) {
      rotate (rad);
    } else if (r > 0.40 && r < 0.50) {
      rotate (rad*3);
    } else if (r > 0.50 && r < 0.65) {
      rotate (rad*2);
    } else {
      rotate (rad*4);
    }

    arcPlusColor(grays[int(random(grays.length))]);
  }
}
//----------------------------------------

void arcPlusColor (color arcColor) {
  fill (arcColor);
  arc (0, -100, 200, 200, 0, PI);
}

class HalfmoonRect {

  color c1 = #FFFFFF;
  color c2 = #BEBEBE;
  color c3 = #7F7F7F;
  color c4 = #3F3F3F;
  color c5 = #000000;

  color [] grays = {c1, c2, c3, c4, c5};
  
  //-----------------------------

void display() {

    noStroke();

    translate (100, 100);

    rectPlusColor(grays[int(random(grays.length))]);
  }
  
  //-----------------------------

void rectPlusColor (color rectColor) {
    fill (rectColor);
    rectMode (CENTER);
    rect (0, 0, 200, 200);
  }
}

```

---

<div class="post-metadata">

**Author:** ![slow\_izzm](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/slow_izzm/32/5637_2.png) [@slow\_izzm](https://discourse.processing.org/u/slow_izzm)\
**Post date:** [April 20, 2020, 11:14pm UTC](https://discourse.processing.org/t/trying-to-convert-2-objects-into-a-super-class-and-a-subclass/19977/2 "2020-04-20T23:14:14Z")

</div>

Create a base class, then use `extends` on your classes you want to inherit from base.

[https://processing.org/reference/extends.html](https://processing.org/reference/extends.html)  
[https://processing.org/reference/super.html](https://processing.org/reference/super.html)

```auto
HalfmoonRect hlfMn_R;
HalfmoonArc hlfMn_A;

void setup() {
  size(200, 200);

  hlfMn_R = new HalfmoonRect();
  hlfMn_A = new HalfmoonArc();

  frameRate(1);
}

void draw() {

  background (255);

  hlfMn_R.display();
  hlfMn_A.display();
}

```

```auto
class Halfmoon {

  color c1 = #FFFFFF, 
    c2 = #BEBEBE, 
    c3 = #7F7F7F, 
    c4 = #3F3F3F, 
    c5 = #000000;
  color[] grays = {c1, c2, c3, c4, c5};  

  float deg, rad;

  Halfmoon() {    
    deg = radians(90);
  }

  void arcPlusColor (color arcColor) {
    fill (arcColor);
    arc (0, -100, 200, 200, 0, PI);
  }

  void rectPlusColor (color rectColor) {
    fill (rectColor);
    rectMode (CENTER);
    rect (0, 0, 200, 200);
  }
}

```

```auto
class HalfmoonArc extends Halfmoon {
  HalfmoonArc() {
    super();
  }

  void display() {
    noStroke();
    float r = random(1);
    println (r);

    if (r < 0.40) {
      rotate (deg);
    } else if (r > 0.40 && r < 0.50) {
      rotate (deg*3);
    } else if (r > 0.50 && r < 0.65) {
      rotate (deg*2);
    } else {
      rotate (deg*4);
    }

    arcPlusColor(grays[int(random(grays.length))]);
  }
}

```

```auto
class HalfmoonRect extends Halfmoon {

  HalfmoonRect() {
    super();
  }

  void display() {
    noStroke();
    translate (100, 100);
    rectPlusColor(grays[int(random(grays.length))]);
  }
}

```

---

<div class="post-metadata">

**Author:** ![debxyz](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@debxyz](https://discourse.processing.org/u/debxyz)\
**Post date:** [April 21, 2020, 12:21am UTC](https://discourse.processing.org/t/trying-to-convert-2-objects-into-a-super-class-and-a-subclass/19977/3 "2020-04-21T00:21:58Z")

</div>

Thank you @slow_izzm for this!!  
I now see what I was doing wrong!  
🤓
