posted 23 years ago
Hi I'm working through 'Complete Java2 Certification'. Can anyone tell me why the following code is illegal:
class Complex{
private double real, imaginary;
public Complex(double r, double i){
real = r;
imaginary = i;
}
public Complex add(Complex c){
return new Complex(real + c.real, imaginary + c.imaginary);
}
}
class Client{
void useThem(){
Complex c1 = new Complex(1, 2);
Complex c2 = new Complex(3, 4);
Complex c3 = new c1.add(c2);
double d = c3.real; //Illegal!
}
}
Why is the above line illegal?
Even though 'real' is private, it's being accessed via an instance of Complex.
Any help would be much appreciated.
The early bird may get the worm, but the second mouse gets the cheese.........<br /> <br />Sun Certified Programmer for Java 2 Platform<br />Sun Certified Web-Component Developer for J2EE Platform