posted 18 years ago
class GFC500 {private String name;}
class GFC501 {
private String name;
private void setName(String name) {this.name = name;}
private String getName() {return name;}
}
class GFC502 {
private String name;
public void setName(String name) {this.name = name;}
public String getName() {return name;}
}
Which class is not tightly encapsulated?
a. GFC501
b. GFC502
c. GFC503
d. None of the above
GFC500 totally encapsulated because you can't access the member 'name' at all.
GFC501 totally encapsulated because you can't access the private member 'name' with a private method. No different than GFC500
GFC502, while it is encapsulated, it's not a good encapsulation because the set method allows any value to be set for name. It's as if the member 'name' were public.
Tight encapsulation will not only protect direct acess to data members, but will also prevent those members from being set to improper values. Signs of tight encapsulation might be: Read-only members (only has a getter), or setters that look like:
where the integrity of the new value is checked in some way.
Hope that helps you
SCJP 1.4, SCWCD J2EE 1.4, SCJD J2SE 1.5, SCBCD J2EE 1.3, SCDJWS (In Progress)