We can create another kind of circle: a numbered circle that behaves like a circle, but displays a unique number on each circle.
We can create another kind of circle: a numbered circle that behaves like a circle, but displays a unique number on each circle.
We can create another kind of circle: a numbered circle that behaves like a circle, but displays a unique number on each circle.
To do this, display the number just before drawing the circle itself. We use the x and y variables that we want to inherit from the Circle superclass. This means that x and y are no longer private to the Circle class, but they must be protected instead.
To do this, display the number just before drawing the circle itself. We use the x and y variables that we want to inherit from the Circle superclass. This means that x and y are no longer private to the Circle class, but they must be protected instead.
To do this, display the number just before drawing the circle itself. We use the x and y variables that we want to inherit from the Circle superclass. This means that x and y are no longer private to the Circle class, but they must be protected instead.
This mynumber variable is an extra piece of state to add to the new subclass.
This mynumber variable is an extra piece of state to add to the new subclass.
This mynumber variable is an extra piece of state to add to the new subclass.
Its value must be set when the new NumberedCircle is initialised, in the constructor.
Its value must be set when the new NumberedCircle is initialised, in the constructor.
Its value must be set when the new NumberedCircle is initialised, in the constructor.
But what value should it be given? We ned a value that is 1 for the first circle, 2 for the second and so on. We can declare a class variable that is incremented each time a new NumberedCircle is created.
But what value should it be given? We ned a value that is 1 for the first circle, 2 for the second and so on. We can declare a class variable that is incremented each time a new NumberedCircle is created.
But what value should it be given? We ned a value that is 1 for the first circle, 2 for the second and so on. We can declare a class variable that is incremented each time a new NumberedCircle is created.
But what value should it be given? We ned a value that is 1 for the first circle, 2 for the second and so on. We can declare a class variable that is incremented each time a new NumberedCircle is created.
But what value should it be given? We ned a value that is 1 for the first circle, 2 for the second and so on. We can declare a class variable that is incremented each time a new NumberedCircle is created.
But hang on, a new NumberedCircle gets initialised as far as numbering is concerned, but how about the more fundamental Circle construction? Our new constructor must invoke the existing superclass constructor as follows:
But hang on, a new NumberedCircle gets initialised as far as numbering is concerned, but how about the more fundamental Circle construction? Our new constructor must invoke the existing superclass constructor as follows:
But hang on, a new NumberedCircle gets initialised as far as numbering is concerned, but how about the more fundamental Circle construction? Our new constructor must invoke the existing superclass constructor as follows:
Finished code.
class RedCircle extends Circle {
public void draw(java.awt.Graphics g){
g.setColor(java.awt.Color.red);
super.draw(g);
}
}
class RedCircle extends Circle {
public void draw(java.awt.Graphics g){
g.setColor(java.awt.Color.red);
super.draw(g);
}
}
class NumberedCircle extends Circle {
public void draw(java.awt.Graphics g){
g.setColor(java.awt.Color.red);
super.draw(g);
}
}
class NumberedCircle extends Circle {
public void draw(java.awt.Graphics g){
g.setColor(java.awt.Color.red);
super.draw(g);
}
}
class NumberedCircle extends Circle {
public void draw(java.awt.Graphics g){
g.setColor(java.awt.Color.red);
super.draw(g);
}
}
class NumberedCircle extends Circle {
public void draw(java.awt.Graphics g){
g.drawString("Number "+mynumber, x+10, y);
super.draw(g);
}
}
class NumberedCircle extends Circle {
public void draw(java.awt.Graphics g){
g.drawString("Number "+mynumber, x+10, y);
super.draw(g);
}
}
class NumberedCircle extends Circle {
*public void draw(java.awt.Graphics g){
g.drawString("Number "+mynumber, x+10, y);
super.draw(g);
}
}
class NumberedCircle extends Circle {
int mynumber;
public void draw(java.awt.Graphics g){
g.drawString("Number "+mynumber, x+10, y);
super.draw(g);
}
}
class NumberedCircle extends Circle {
int mynumber;
public void draw(java.awt.Graphics g){
g.drawString("Number "+mynumber, x+10, y);
super.draw(g);
}
}
class NumberedCircle extends Circle {
int mynumber;
*public void draw(java.awt.Graphics g){
g.drawString("Number "+mynumber, x+10, y);
super.draw(g);
}
}
class NumberedCircle extends Circle {
int mynumber;
public NumberedCircle(){
mynumber=???;
}
public void draw(java.awt.Graphics g){
g.drawString("Number "+mynumber, x+10, y);
super.draw(g);
}
}
class NumberedCircle extends Circle {
int mynumber;
public NumberedCircle(){
mynumber=???;
}
public void draw(java.awt.Graphics g){
g.drawString("Number "+mynumber, x+10, y);
super.draw(g);
}
}
class NumberedCircle extends Circle {
int mynumber;
public NumberedCircle(){
mynumber=???;
}
public void draw(java.awt.Graphics g){
g.drawString("Number "+mynumber, x+10, y);
super.draw(g);
}
}
class NumberedCircle extends Circle {
int mynumber;
public NumberedCircle(){
mynumber=nthCircle++;
}
public void draw(java.awt.Graphics g){
g.drawString("Number "+mynumber, x+10, y);
super.draw(g);
}
}
class NumberedCircle extends Circle {
int mynumber;
*public NumberedCircle(){
mynumber=nthCircle++;
}
public void draw(java.awt.Graphics g){
g.drawString("Number "+mynumber, x+10, y);
super.draw(g);
}
}
class NumberedCircle extends Circle {
int mynumber;
static int nthCircle=1;
public NumberedCircle(){
mynumber=nthCircle++;
}
public void draw(java.awt.Graphics g){
g.drawString("Number "+mynumber, x+10, y);
super.draw(g);
}
}
class NumberedCircle extends Circle {
int mynumber;
static int nthCircle=1;
public NumberedCircle(){
mynumber=nthCircle++;
}
public void draw(java.awt.Graphics g){
g.drawString("Number "+mynumber, x+10, y);
super.draw(g);
}
}
class NumberedCircle extends Circle {
int mynumber;
static int nthCircle=1;
public NumberedCircle(){
*mynumber=nthCircle++;
}
public void draw(java.awt.Graphics g){
g.drawString("Number "+mynumber, x+10, y);
super.draw(g);
}
}
class NumberedCircle extends Circle {
int mynumber;
static int nthCircle=1;
public NumberedCircle(){
super();
mynumber=nthCircle++;
}
public void draw(java.awt.Graphics g){
g.drawString("Number "+mynumber, x+10, y);
super.draw(g);
}
}
class NumberedCircle extends Circle {
int mynumber;
static int nthCircle=1;
public NumberedCircle(){
super();
mynumber=nthCircle++;
}
public void draw(java.awt.Graphics g){
g.drawString("Number "+mynumber, x+10, y);
super.draw(g);
}
}