aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Access Modifiers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Access Modifiers" Watch "Access Modifiers" New topic
Author

Access Modifiers

Samira Mastali
Ranch Hand

Joined: Aug 03, 2001
Posts: 74
Hi Folks,
Can anybody explain me why the following line in the 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 = c1.add(c2);
double d = c3.real; // Illegal!
}
}
what I don't understand is that c3 is an instance of class complex and we can have access to a private member of a class through an instance of that.What is difference between c3 and c1 and c2?
Thanks,
Samira
rahul kumar
Ranch Hand

Joined: Feb 24, 2001
Posts: 75
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 = c1.add(c2);
double d = c3.real; // Illegal!
}
}
Although c3 is an instance of complex but private variables can't be acessed outside the class.real is private variable thatwhy it can't be accessed in other class client
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Access Modifiers
 
Similar Threads
Possible error in CJ2CSG?
Access Modifiers
why this code fragment is illegal?
Doubt in RHE page 78(new edition)
modifiers