Sunday, January 9, 2011

encapsulation and abstraction ?

Q1: encapsulation is a type of "information hiding" in which you hide data attributes of a class behind getter and setter methods; this allows you to control how data is stored and manipulated.  For example you could have a class that holds an IP address (for a networking application).  The class might expose the IP address as a String, such as "127.0.0.1" with methods like getIpAddr():String and setIpAddr(String) but internally the ipAddr field could be stored as a Java int.

Whereas, "abstraction" is a concept about modeling: to create an abstraction is to find the "minimal amount of information" about the thing being modeled to solve the requirements of that "thing".