Is encapsulation possible without OOP?
Asked Answered
F

1

1

I was asked a question in an interview: if encapsulation is possible without OOP, e.g. in a procedural language?

Frizzell answered 4/10, 2018 at 11:48 Comment(5)
If a module provides some services, other modules requesting those services (a.k.a. clients) do not have access to data structures that are not publicly exposed.Regatta
Do you consider struct/record to be OOP or procedural?Sf
possibly but not in language level, for example in early edge of PHP developers use for example __ at the begin of method name means that is private method oop it's just how code is organizedTrapper
What answer did you give in the interview?Urbanus
I answered that it is not possible without oop as it is one of the important functionality of oop. If it is possible without oop then there is no need of oop :-PFrizzell
U
4

Bob Martin has stated that encapsulation is not only possible without OOP, it was better before OOP came along.

Here is an excerpt from a talk he gave in 2014 at Yale School of Management.

We had perfect encapsulation. In C, all you had to do was forward declare your functions and your data structures. You didn't have to implement them. You would forward declare them in a header file, and then you would implement them in a C file. Your users would #include your header file. They could see nothing of your implementation. Perfect encapaulation. There was no way any of your users could see any of your data values. All they could see was your function signatures. They could see the names of your data structures but none of the members inside your data structures. Absolutely perfect encapsulation.

Urbanus answered 9/10, 2018 at 18:23 Comment(2)
But they asked in general sense, not for a specific languageFrizzell
That's somehow a weak concept: encapsulation by obfuscation. If user has knowledge of internal structure, nothing prevent to access underlying data by using appropriately crafted header instead of the public one. In some languages like Smalltalk, you can only interact with another object by sending a message to it, no way to access its data by *((char *) foo+offset).Attraction

© 2022 - 2025 — McMap. All rights reserved.