OOP has not invented encapsulation
Few days ago, I was watching a talk in which the speaker was presenting the history of the software engineering field of the last fifty years.
At some point he has stated that "OOP introduced (encapsulation)(https://en.wikipedia.org/wiki/Encapsulation_(computer_programming))".
For reference, encapsulation is a technique promoting decouple by abstracting (hiding) the data structures and algorithms behind interfaces.
At the beginning of my career I was working on an operating system for SIM cards, it was mostly C and Java Card (a kind of Java 1.4/1.5).
We had to support processor designs, chipsets, and customer customizations.
In order to achieve that, we relied on C headers separation mechanism, let's say we have an interface which deals with "environment" (whatever it means).
We can have a really simple program:
int
env.h
is a basic header file which describes the interface:
typedef struct _env_t env_t;
env_t *;
void ;
/* !ENV_H_ */
We have one abstract type env_t
(struct _env_t
) and two functions (get_env
and display_env
).
Then we can have two implementations:
;
env_t *
void
And another more "complex" one:
;
env_t *
void
We can compile them
$ cc -c *.c
And finally link them:
$ cc -o a main.o enva.o
$ cc -o b main.o envb.o
Let's see the results:
$ ./a
enva: 42
$ ./b
envb: 4.2
Here we are, compile-time (well, linking-time) encapsulation.
On another hand, we can achieve run-time we can rely on function pointers:
typedef struct _env_t env_t;
Then we can define behaviors:
void
env_t *
void
env_t *
Let's use it:
int
enva: 42d
envb: 4.2d
I think encapsulation is quite old, older than OOP at least.