Friday, October 24, 2008

Section 1.4: Information Hiding and Encapsulation

Requirement: Describe information hiding (using private attributes and methods), encapsulation, and exposing object functionality using public methods; and describe the JavaBeans conventions for setter and getter methods.


Encapsulation:

(1.4.1) Encapsulation is sometimes called information hiding so they are the same concept. (1.4.2) Encapsulation is nothing more than combining data and behavior in one package and hiding the implementation detials from the user of the object. (1.4.3) The key to making encapsulation work is to have methods never access instance fields in a class other than their own. (1.4.4) Encapsulation is the way to give the object its black box bahvior, which is the key to reliability and reuse. (1.4.5) Benifits of encapsulation:

  1. You can change the internal implementation without affecting any code other than the methods of the class.
  2. Mutator methods (methods that assign values to private instance fields, the setter methods) can perform error-checking, whereas code that simply assigns to a filed may not go to the trouble.

(1.4.6) When creating accessor methods (getter methods) that get the value of a private reference field, never return the reference to the member field directly, instead return a clone of that member object.

Ex:
Assuming that hireDate is a member field of type Date:
Don't write the following code:
public getHireDate() {
reutrn hireDate;
}


In the above code, the returned Date object is the same as the hireDate member field. So any outer code that calls getHireDate can modify the hireDate member field which violates encapsulation.
Instead, write the following code:
public getHireDate() {
reuturn (Date) hireDate.clone();
}



The private Keyword:

(1.4.7) The private keyword makes sure that the only methods that can access the instance fields or methods declared as private are the methods of the class itself. (1.4.8) Instance fields should be explicitly declared private or they will be considered to have the package visibilty which breaks encapsulation. (1.4.9) A method can access the private data of all objects of its class.

Ex:
class Employee {
public equals(Employee other) {
reutrn name.equals(other.name);
}

private String name;
}


(1.4.10) The methods of a subclass cannot access the private fields of its superclass. (1.4.11) Sometimes, you may wish to break up the code for a computation to seprarate helper methods. Typically, these helper methods shouldn't be part of the public interface - they may be too close to the current implementation or require a special protocol or calling order. Such methods are best implemented as private. (1.4.12) Only inner classes can be declared as private, regular classes are either public or have the default package visibilty.


The public Keyword:

(1.4.13) According to the Java Language Specification the main method must be declared as public. (1.4.14) When you override a method, the subclass metohd must be at least as visible as the superclass method. In particlular, if the superclass method is declared as public the subclass method must also be declared as public.


JavaBeans Conventions for Setter and Getter Methods:

(1.4.15) A bean defines an intance field p of type T, the setter and getter methods should have the following convention:

  1. Getter: public T getP()
  2. Boolean getter: public boolean isP()
  3. Setter: public void setP()

p may consist of many words in which case the first letter of each word will be capital except for the first letter of the first word. However, the name of p after the get, set, or is part should be capitalized in the first letter of every word (ex: private String firstName; while public String getFirstName()). (1.4.16) For indexed instance fields (array type instance fields) should follow the following conventions:

  1. Array getter: public T[] getP()
  2. Array element getter: public T getP(int)
  3. Array setter: public void setP(T[])
  4. Array element setter: public void setP(int,T)

(Approximately all of the material written here is taken from the references mentioned in the first post. I don't claim to have authored any part of this material . I just compile my own notes from one or many of these references).

1 comment:

Tchulda said...

Keep up the good work
When creating accessor methods (getter methods) that get the value of a private reference field, never return the reference to the member field directly, instead return a clone of that member object.
This is very interesting