// Example: Inheritance in OOP // Vocabulary: Superclass, Subclass, inheritance // Processing: extends, super(), super.XX // // Circles and Squares are types of Widgets // So we define an abstratc class Widget with all attributes and methods // common to all Widgets. Next we define two subclasses: Square, and Circle // that are subclasses...to explore the concepts
// In this sketch, there are 10 Square objects and 10 Circles // So we have a total of 20 Widgets int N = 20; Widget[] widgets;
void setup() { size(800, 800);
// The widgets[] array holds Square as well as Circle objects widgets = new Widget[N]; // Next create N/2 Square objects for (int i = 0; i < N/2; i++) { widgets[i] = new Square(random(width), random(height), random(20, 100)); } // Next, create N/2 Circle objects for (int i = N/2; i < N; i++) { widgets[i] = new Circle(random(width), random(height), random(20, 100), color(random(255), random(255), random(255))); } } // setup()
void draw() { background(255);
for (Widget w : widgets) { w.display(); w.jiggle(); } } // draw()