in Java
Encapsulation is the packing of data and functions into a single component. It allows selective hiding of properties and methods in an object by building an impenetrable wall to protect the code from accidental corruption.
Suppose a newly hired SYSOP is responsible for your passward security...
class Record {
String name;
String password;
}
public class RecordDemo {
public static void main(String[] argv) {
Record r;
String s, p;
s = getLoginNameFromUser();
r = getRecordFromFile(s);
System.out.println(r.password);
p = getPasswordFromUser();
if(p.equals(r.password)){
//...
}
}
}
If password not encoded, the SYSOP might easily get your password by getRecordFromFile.
class Record {
String name;
String encoded_password;
}
public class RecordDemo {
public static void main(String[] argv) {
Record r;
String s, p;
s = getLoginNameFromUser();
r = getRecordFromFile(s);
p = getPasswordFromUser();
if (YOUR_ENCOING(p).equals(r.encoded_password)) {
//...
}
//A new and careless programmer adds this line
r.encoded_password = null;
}
}
Even when password encoded, a careless programmer may make stupid bugs.
class Record {
private String encoded_password;
public String get_encoded_password() {
return encoded_password;
}
}
public class RecordDemo {
public static void main(String[] argv) {
Record r;
String s, p;
s = getLoginNameFromUser;
r = getRecordFromFile(s);
p = getPasswordFromUser;
if (YOUR_ENODING(p).equals(r.get_encoded_password())){
//...
}
// A new and careless programmer adds this line
r.encoded_password = null; //won't work
}
}
But what if you want to set a new password?
Better
class Record {
String name;
private String encoded_password;
public String get_encoded_password() {
return encoded_password;
}
public void set_encoded_password(String raw_password) {
if(blahblah)
encoded_password = YOUR_ENCODING(raw_password);
}
}
As a designer, you should avoid giving the users of your code too much freedom to do bad and/or make bugs
You implement the Record class, and other people (possibly you after two years) use it.
Separate implementation and use:
Silly mistakes can happen.
(a.k.a. instance variables).
Check them in the methods.
Don't trust other people:
Hide unnecessary details:
Think about possible correct/incorrect use of your class:
Here are things you should care about
class Record{
private String name;
public String get_name() {
return name;
}
public String get_copied_name() {
return new String(name);
}
}
class Record {
private String name;
public void set_name(String name) {
if(name != null)
this.name = name;
}
}
Accessor and Mutator
Mutator:
Check and set the instance variable to a value.
Accessor:
Get the content of the instance.
More on hiding details
class Date { //implemented for your desktop
private int month, day;
public int get_month() {
return month;
}
public int get_day() {
return day;
}
class Date_TVVO { //implemented on a small—memory machine
private short encoded_month_and_day;
public int get_month {
return encoded_month_and_day / 100;
}
public int get_day() {
return encoded_month_and_day % 100;
}
}
Two implementations, same behavior—easy for users to switch on different machines.
Some rules of thumb
Cons
Pros
Coupling and Cohesion
In software engineering, coupling or dependency is the degree to which each program module relies on each one of the other modules.
Coupling is usually contrasted with cohesion. Low coupling often correlates with high cohesion, and vice versa. Low coupling is often a sign of a well-structured computer system and a good design, and when combined with high cohesion, supports the general goals of high readability and maintainability.
Abstraction?
Abstraction is the process of separating ideas from specific instances of those ideas at work. Computational structures are defined by their meanings (semantics), while hiding away the details of how they work. Abstraction tries to factor out details from a common pattern so that programmers can work close to the level of human thought, leaving out details which matter in practice.
public abstract class Shape
{
public double getArea();
}
public class Triangle entends Shape
{
public double a;
public double b;
public double c;
public double getArea
{
//return triangle's area
}
}
public class Rectangle entends Shape
{
public double a;
public double b;
public double getArea()
{
//return rectangle's area
}
}
public class ShapeDemo {
public static void main(String[] argv) {
List<Shape> shapes = ListShapes() // contains circles, triangles and rectangles
double area = 0;
foreach(Shape shape in shapes)
area += shape.getArea();
// do something useful with area here
}
}
Example of Abstraction
In the context where we don 't care about specific properties of object we can handle all extended objects as Shape.