Log in

View Full Version : why encapsulation is required?


kim123
03-02-2009, 05:33
hi,

Please tell me why encapsulation is required?

Thanks

kiettyyyy
03-02-2009, 05:45
Encapsulation in regard to????

Jared Russell
03-02-2009, 07:42
http://en.wikipedia.org/wiki/Encapsulation_(computer_science)

Analog
03-02-2009, 07:44
hi,

Please tell me why encapsulation is required?

Thanks

Be more descriptive.

JamesBrown
03-02-2009, 08:38
From Wikipedia:

Encapsulation is the hiding of information. C++ implements encapsulation by allowing all members of a class to be declared as either public, private, or protected. A public member of the class is accessible to any function. A private member is accessible only to functions that are members of that class and to functions and classes explicitly granted access permission by the class ("friends"). A protected member is accessible to members of classes that inherit from the class in addition to the class itself and any friends.

The OO principle is that all of the functions (and only the functions) that access the internal representation of a type should be encapsulated within the type definition. C++ supports this (via member functions and friend functions), but does not enforce it: the programmer can declare parts or all of the representation of a type to be public, and is allowed to make public entities that are not part of the representation of the type. Because of this, C++ supports not just OO programming, but other weaker decomposition paradigms, like modular programming.

It is generally considered good practice to make all data private or protected, and to make public only those functions that are part of a minimal interface for users of the class. This hides all the details of data implementation, allowing the designer to later fundamentally change the implementation without changing the interface in any way




In general whenever you are writing an API, or any interface for other people to use encapsulation is a good practice. without encapsulation teams would have to rewrite their code every time WPILib made changes to a function that they use. With encapsulation if the inputs and outputs to a function remain the same then WPILib can be changed without effecting teams.

pheadxdll
03-02-2009, 09:02
hi,

Please tell me why encapsulation is required?

Thanks


Recent practical example: C/C++ Team Update #3. Encapsulation allows updates to the WPILib without borking your program. It's a common coding practice in industry. :)

Mageofdancingdr
03-02-2009, 09:28
FIRST is supposed to be a simulation of a real engineering experience. real engineers use encapsulation because it is more secure against malicious programmers and their programs. This is also why proper memory allocation is important as well. Keeping code secure from outside influence is important so that
A) the code doesn't interfere with other processes on the same machine
B) outside programmers can't access it
C) it allows for a more modular type of code where simple function calls change not entire blocks of code.

galewind
03-02-2009, 10:06
Here's an example of why encapsulation is important.

Let us, for a moment, assume that you have variables within an object that are directly related to other variables within that same object. Thus, changing one requires that you adjust others in order to compensate for the change. The object itself should ultimately be responsible for updating itself to make sure that the data correlates accordingly.

Simple example: Assume we made a Rectangle class that contained four variables - length, width, area, perimeter. Please note that area and perimeter are derived from length and width (normally I wouldn't make variables for them), so that changing length would change both area and perimeter.

If you did not use proper information hiding (encapsulation), then another program utilizing that Rectangle class could alter the length without altering the area, and you would have an inconsistent Rectangle. Without encapsulation, it would be possible to create a Rectangle with a length of 1 and a width of 3, and have an area of 32345.

Using encapsulation, we can create a function that, if a program wanted to change the length of the rectangle, that the object would appropriately update its area and perimeter without being inconsistent.

Encapsulation eliminates the possibilities for inconsistency, and shifts the responsibility of staying consistent onto the object itself rather than a program utilizing it.

virtuald
05-02-2009, 00:26
Real engineers use encapsulation because it is more secure against malicious programmers and their programs.


No, definitely not. You *could* make a statement that it makes things easier to debug, and thus more secure, but even then that is not strictly true.


This is also why proper memory allocation is important as well. Keeping code secure from outside influence is important so that
A) the code doesn't interfere with other processes on the same machine
B) outside programmers can't access it


While proper memory allocation is a good thing, these are totally different issues. This is a *type* of encapsulation, but it certainly isn't the encapsulation one talks about when talking about programming languages (and really, (B) isn't even true in many situations that we won't go into here... ).

As someone above has pointed out, WPILib is a great example of why encapsulation is a good thing. Clearly it does not contribute to the security of the program.

For example, just because you declare something in a class as 'private' does not mean someone cannot access it (in absolute terms). Generally, accessing private members in a class will not compile correctly -- however if you had a raw pointer to the member then you could easily change the item, despite it being private. Additionally, there are a few ways one could make something that accesses a private member compile if you wanted to be truly nasty: see http://www.gotw.ca/gotw/076.htm if you're interested.


C) it allows for a more modular type of code where simple function calls change not entire blocks of code.

This is the correct answer (though its more generic than just simple function calls).