2

I know that JavaScript doesn't explicitly allow you to have a member variable of an object as private, but you can do that implicitly by using closures.

But is there an object-oriented language that doesn't allow you to have private member variables at all (not explicitly and not implicitly)?

closed as too broad by gnat, Thomas Owens Jul 21 '18 at 1:32

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

4

Python is an example of an OO language that doesn't support private member variables . In Python, the idea of encapsulated values is one only achieved through convention (prepending the variable name with _). Some IDEs might then enforce that convention by hiding those members from public access, but the language itself doesn't.

Of course, one could get pedantic and claim that unless a language offers true encapsulation, then it's not an OO language, in which case Python then doesn't make the grade. Take that line of "reasoning" to its conclusion and then even Java doesn't qualify as encapsulation can be bypassed using reflection.

  • I would be willing to stipulate to reflection as a "workaround" for a language that otherwise qualifies. – Robert Harvey Jul 20 '18 at 17:17
2

Perl enforces privacy of members by convention only. As Larry Wall, the creator of Perl, once wrote:

"Perl doesn't have an infatuation with enforced privacy. It would prefer that you stayed out of its living room because you weren't invited, not because it has a shotgun."

0

Object oriented programming is defined by inheritance, polymorphism and encapsulation. And since private members falls in the category encapsulation, I would say no.

I know there is an ongoing debate of whether or not JavaScript is an OOP language, since it supports Objects and Classes. The same can be said about Python who also support OOP but doesn't have encapsulation (at least up til version 3).

But your question is if any OOP language doesn't support private members- and to me that is contradictions in terms since encapsulation is mandatory in OOP. I'd say NO.

  • 8
    I'm sorry but this is a very shallow definition of OO and also encapsulation. For instance, Smalltalk (to some, the origin of OO) does not have a private modifier, all methods are public. Also, private members and encapsulation are not the same thing. Encapsulation is achieved when the object's implementation can be completely change from the inside without affecting the external behavior. You can have many private members and still leak implementation details that would block you from doing internal changes. – MichelHenrich Jul 20 '18 at 7:46
  • @MichelHenrich I'm sorry but "falling into the category", is not "the same thing". There are a lot more than private [x] to Encapsulation, for sure. And you can implement things wrong in an OO language. But that wasn't the question. OP wants to know if there is an OO language that don't have private members. And to the best of my knowledge, I didn't know of Smalltalk - so I stand corrected here. But looking at the Wikipedia definition of Uncle Bob's definition- OO is shallow (I admit to that) about those three properties as opposed to structured or functional programming. – Benny Skogberg Jul 20 '18 at 13:27
  • Python implements encapsulation via convention, rather than via language features. It has encapsulation and so qualifies as an OO language, without needing a compiler to enforce it via a private keyword. Arbitrary definition of OO problem solved. – David Arno Jul 20 '18 at 15:29
  • @DavidArno Interesting! Would you consider JavaScript an OO language as well? It has the same convention _ for private. And you can use inheritance and polymorphism as well. – Benny Skogberg Jul 20 '18 at 15:43
  • Couldn't Python also implement encapsulation via closures? Functions don't expose local variables directly, but can do so indirectly by returning an inner function that captures the locals. – Andrew Piliser Jul 20 '18 at 18:06
0

I believe Object Pascal, also did not support public/private data members. And CLOS (Common Lisp Object System) - I don't think supported public/private data either.

  • 1
    Object Pascal definitely supports private fields.(At least in the FreePascal and Delphi flavours.) – Frank Shearar Jul 20 '18 at 16:46
  • I was talking about the one I used decades ago from Apple (used for MacApp). I didn't even know anyone else had re-implemented Object Pascal. Cool! – Lewis Pringle Jul 20 '18 at 16:58

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