OO extends question
in
Programming Questions
•
9 months ago
Hi all,
i have a question, im extending a class P which holds xpos and ypos, as i understand when you extend a class you automagicaly have access to its methods and variables, but for some reason, i can only set the variables through a constructor, how come i just cant say xpos = 1; in the extended class like this ?
//bre
i have a question, im extending a class P which holds xpos and ypos, as i understand when you extend a class you automagicaly have access to its methods and variables, but for some reason, i can only set the variables through a constructor, how come i just cant say xpos = 1; in the extended class like this ?
//bre
- class P {
float xpos=0;
float ypos=0;
}
class Test extends P {
// why cant i do it this way ?
xpos = 1;
// this works with constructor
// Test(float x,float y){
// xpos=x;
// ypos=y;
// }
void print() {
println("x "+xpos);
println("y "+ypos);
}
}
Test t = new Test();
//Test t = new Test(1,1);
void setup() {
t.print();
}
1