D.2 Features of OOP (4 hours)

Students should be able to describe the features of OOP that distinguish it from other approaches to computer programming.


Define the term encapsulation.


Teaching Note:

Data and actions are limited to the object in which they are defined.


Sample Question:


JSR Notes:


Encapsulation means to "protect" data of an object, so that it can only be used in ways that the programmer knows the data should be worked with. The way this is done is by only "showing" certain actions which possible, in the public "interface" of the method. So users of that class can only see...

... of methods which are public (in the "public interface"):

- the method name
- the parameter list (number and kind required)
- the return type (or void if there is no return)

Note that with true encapsulation, no attributes can be "seen" by users of that class. The can only see the "gets" and "sets" of those attributes.


So all attributes of an object of a class using encapsulation are private. For example:

private double checkingAccountBalance;

Refer to D.2.4 for the reasons all of this is a good idea.


But finally, if you were to define encapsulation, as stated in the assessment statement, something like the following two sentences would do nicely:

Encapsulation is the protecting of data of in OOP programming, by making attributes themselves private. The attributes are only able to be worked with indirectly through specific, public methods which work with the data in safe ways.