Friday, January 8, 2010

In dealing with Java: What is the relationship between a client of an object and the object's public members?

It's the only problem that I can't answer on this pre-lab assignment, I have for my programming fundamentals class. It's due in 2.5 hours :(In dealing with Java: What is the relationship between a client of an object and the object's public members?
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
  • highlight colors
  • ointment cream
  • No comments:

    Post a Comment