6

I know C++ had private variables inside classes, and Java, C#, Javascript, and others picked up on that and ran with it. Aside from variable scope, most languages before that just made everything visible. Certainly in assembly, all of memory is accessible (except as limited by hardware).

But was C++ the first language to support the idea of encapsulation?

0

4 Answers 4

8

One form of Javascript encapsulation is one of scope. There aren't modules as such - people just stuff things inside a function ((function(){ ... })(this)) and the things inside of that function can't be used outside. You can hide quite a bit in local scope, and many languages have had it (though arguably, without first class functions this isn't enough for encapsulation of data).

Once we start mentioning first class functions and history, we firmly fall into talking about Lisp. And there's a lot there to be said too.

Early Fortran (for the 704) didn't have subroutines or scope. It was Fortran II that introduced these things.

My apologies for writing off Lisp (1958) with a single sentence... and not saying much about Fortran (1957), or Fortran II (1958). Lisp had closures and Fortran II appears to have some lexical scoping...


When you talk about encapsulation and structured programming the name that should jump out at you is Niklaus Wirth.

What we often think of as encapsulation are things like private variables within a module. Sure, you had things that could pop in and out of scope that couldn't be accessed outside of the scope. But you didn't have "here is this module of code that has some things that you just can't see over there".

It was Modula 2 (1978) that brought us syntax like:

DEFINITION MODULE M1;
  EXPORT QUALIFIED a, b, c, P;

Where one could then later access M1.a and M1.b but not M1.d (because that wasn't exported).

You could also do:

MODULE M2;
  FROM M1 IMPORT a, P;

Some of the language looks downright Pythonish...

MODULE Hello;
FROM STextIO IMPORT WriteString;
BEGIN
  WriteString("Hello World!");
END Hello.

This is more than just hiding something within some scope to not clobber name space. This is hiding the visibility of some variables, procedures or functions from other modules. (Tutorial on modules within Modula-2) This type of encapsulation is what we often think of now and see in languages like C++ (1983), Java (1995), and C# (2000).


Modula wasn't the only language to have some form of data hiding. Simula was an early object oriented language that had some data encapsulation:

Simula protection block

This particular extension was from a later extension to the language from around 1972. This made its way into the standard of Simula 87 (the original Simula 67 had everything as public and lacked encapsulation). This language a strong influence on C++ (A Conversation with Bjarne Stroustrup, Why I Created C++).

This protection block for an identifier allowed a class to specify if its subclasses - Hidden could only be used in the class where its declared and Protected could only be used where it was declared and its subclasses.


One more mention from another old object oriented language - Smalltalk (1972).

In Smalltalk, all instance variables were protected (visible only to the class and its subclasses) and all methods were public (Smalltalk object model). This could be used to preform some encapsulation.

It was considered bad form to access an instance variable of the superclass from a subclass, or to invoke a method in the 'private' category).


Related reading:

3
  • Thank you for: "But you didn't have 'here is this module of code that has some things that you just can't see over there'"... that captures the sense of what I was wondering about.
    – sea-rob
    Jul 30, 2014 at 23:12
  • Simula-67 predates all this OO stuff.
    – david.pfx
    Jul 31, 2014 at 14:30
  • 1
    @david.pfx the original specification of Simula-67 was all public. Hidden and Protected was something added later (about 1972) which was contemporary with Smalltalk and Modula. I could find no mention of these keywords in the original documentation I read 1, 2. Documentation written after that did have data hiding 3
    – user40980
    Jul 31, 2014 at 15:49
4

The idea of Local Scope goes all the way back to the 1950's. Fortran has it, Lisp has it.

See Scope (Computer Science).

2

As the other guy said, local variables go all the way back to FORTRAN.

The groundwork for what we now call "object-oriented programming", specifically classes with methods and public and private instance variables, was laid down in Simula 67 by Ole-Johan Dahl. He wrote a good bit of it up in his section of "Structured Programming" [Dahl, Dijkstra & Hoare]. (No, you CAN'T borrow my copy!)

2

If you're talking about encapsulation as used in modern OO languages, the clearest and earliest language specification is found in Simula-67. All the key OO features are there, and the language was widely used within a specific community.

If you're talking about encapsulation as equivalent to scope then the clearest and earliest language specification is Algol 58 (aka IAL). The Algol 60 spec was widely implemented, and contains the concepts of function scope and block scope that are now found in all C-like languages.

Around the same time FORTRAN had variables local to subroutines and shared COMMON but not block scope.

Also around the same time Lisp offered dynamic scope and a kind of closure but not lexical scope or encapsulation as we now understand it.

Not the answer you're looking for? Browse other questions tagged or ask your own question.