class MyObject {
public String isdnNum;
private double price;
// constructor
public MyObject( String isdn, double amt ) {
isdnNum = isdn;
price = amt;
}
// method
public String getPrice() {
return price;
}
}
In the above example client can only see the public members.
MyObject obj = new MyObject( ';123456';, 9.95 );
client can see the isdn as obj.isdnNum;
client cannot see price as obj.price, instead
double thePrice = obj.getPrice();
this example is very simple, you normally would not expose price that way, but it shows if the value 'price' is private, the only access is to use a member.method() and programmers carefully code the access modifiers: default, public, private, protected
No comments:
Post a Comment