5 replies
by wahida
Something which I gathered during college days and when I taught in NIIT ..
Definition: Encapsulation refers to a form of protected aggregation. Data is said to be encapsulated in a class if the data cannot be directly accessed.
In some sense, encapsulation is a form of indirection, because it discourages direct access to data. At this point, we can examine a hypothetical class, a Date class for representing dates. A possible interface:
class Date
{
private:
int Day, Month, Year;
public:
Date();
void print();
void setYear(int);
void setMonth(int);
void setDay(int);
int year();
int month();
int day();
};
This might not appear to offer much encapsulation. However, the fact that we indirectly accessdata through methods immediately gives rise to one possible advantage: the set methods can perform range checks. Range checking makes it possible to preserve data integrity and maintain a consistent state for each object.
The notion of maintainingan object in a consistent state can often be captured in a class invariant A class invariant is a statement that should always be true. For example,
Day > 0 && Day <= 31 && Month > 0 && Month <= 12
One could write a more sophisticated version of this invariant (for example, February can't have more than 28 days. These subtleties could be accounted for)
Sometimes, it's even convenient to write functions that check class invariants, for debugging purposes.
by jack_swayze
"Data is said to be encapsulated in a class if the data cannot be directly accessed"
This means that, in order to re-use the data, you have to access the program (object code).
This makes the data very inflexable and when you can actually re-use the data it makes that re-use more costly.
It almost totally eliminates the option of using 'plug in' tools such as MS Access to access the data, or Data Warehousing tools.
The integrity checks advocated can be achieved by storing the datain a good relational database (such as Oracle).
In short, encapsulation is the idea that the code produced by the programmer should take on much, if not all, of the responsibility that has traditionally been done by a good DBMS.
Or, to put itanother way, encapsulation is the means for the programmer to assert control over the data where that control traditionally belongs to a good DBA.
Now, the more complex the whole system is, the more costly encapsulation will be. If there is onlyone, or two, coders on the development staff, and the DBA is spread very thin, then maybe encapsulation makes sense. But if the project is just one phase out of a multi-year phased activity involving coders numbered in the dozens, does it really make sense for the coders to be so possessive of the data?
by epepke
Encapsulation is primarily for program variables. For example, a button might encapsulate its position on the screen, its activation state, the text/image on the button, etc. I doubt that too many people would make a database call every time the button needs to be redrawn.
Like anything, though, people take encapsulation to extremes. The whole idea that variables need set and get methods, taught in all the books, is completely squirrely. If there is a one-to-one correspondence between methods and variables, it's usually a bad design. The button, for example, should have a move method (or something like that) which handles erasing the button, redrawing it, calculating and queueing the appropriate region to update the window, etc. One doesn't simply make the coordinates public, because changing them has an undefined effect. Most likely, the button would not move but would appear to move on the next redraw, leaving its old self as a ghost.
That having been said, there are data-based encapsulation systems, but they haven't been popular. Smalltalk had one, and so did some variants of LISP. I like this way of doing things so much that I implemented it in a 300,000 line visualization package. Every object variable could depend on any number of other variables and could be automatically recalculated when needed. It solved a tremendous number of problems.
by epepke
Like much in OO, it's a fancy word.
Basically, it means that data and methods are located in one place, and there are rules about where they can be accessed and/or set.
In OO this is done by keeping the data and methods within objects and using declarations such as private and protected to limit access from outside the object.
Although the word is used a lot with OO, it's just one phase in an effort that has been going on as long as there has been computer science. In the bad old days, programs just had a bunch of global variables and code, and this led to maintenance nightmares. Procedures, structured languages, scoping rules, local variables and functions, and now objects have all been stops on the road to encapsulation.
by generalist
It is good to see in writing what I have suspected over the years.
With the exception of objects and encapsulation, I have used the 'stops on the road' for a couple of decades. If you do things right you can avoid a lot of programming hassles.
Of course it takes a certain amount of creativity to see such things and a certain amount of discipline to use the techniques. People will ask you why you do some things when other things appear to be easier. When you explain that your 'meta-rules' eliminate common problems, they'll nod and continue using their own techniques.
In the long run, obeying the 'meta-rules' takes longer front end but is less prone to errors. Once you have a code library built up, you can do the equivalent of drop and drag and be sure that the imported code doesn't interfere with itself and the new code.
Objects and encapsulation just turn the 'meta-rules' into officially recognized rules that are enforced by the OOP language.
Now if I could only getsome 'official' experience with OOP to that it appears on my resume and is tied to a company as opposed to a hobby...
This discussion is closed to new replies.
Trending IT Employment posts
Sort by Trending