thank you bro
it worked
I learned 2 new things
the first one is pretty obvious but for some reason, it did not click in my mind that I can create an array that contains the new class that I created
the second thing is that the subclass doesn’t inherit the Constructor
abstract class shape {
float x,y;
shape(){
x=random(width);
y=random(height);
println("this is shape");
}
void jiggle(){
float speed =3;
x+=speed*random(-1,1);
y+=speed*random(-1,1);
}
abstract void display();
}
class circle extends shape{
circle(){
x=random(width);
y=random(height);}
float d=30;
circle (float id ){
d=id;
println(" this is circle ");
}
void display(){
ellipse(x,y,d,d);
}
float rex() {
return x;}
}
class square extends shape{
float b=30;
square(){
x=random(width);
y=random(height);}
square (float ib ){
b=ib;
println(" this is square ");
}
void display(){
square(x,y,b);
}
}
circle a;
square b;
shape [] arrc;
shape [] arrs;
void setup(){
size(800,600);
arrc = new shape [10];
for ( int i =0;i<arrc.length;i++) {
arrc[i]=new circle();
}
arrs = new shape [10];
for ( int i =0;i<arrs.length;i++) {
arrs[i]= new square() ;
print(arrs[i]);
}
a = new circle(25);
b = new square(50);
}
void draw(){
background(192);
for ( int i =0;i<arrs.length;i++) {
arrc[i].display();
arrc[i].jiggle();
arrs[i].display();
arrs[i].jiggle();}
}
One of the points of using a shared base class is that you don’t need to have separate arrays of squares and circles. You can have just one array of the base class type and add to it objects of either subclass.