Encapsulation is the hiding of an implementation in OOP the way I understand it. I searched on Google and I was thinking of trying to write a program to test, but my tests would only happen at run time. Python allows you to hide methods with "__" syntax. Java allows you to hide member variables with the "private" keyword. Does this hiding happen at compile time or run time?

  • What would hiding happening at compile time mean? Does it mean hiding something from the compiler? If so, well, guess what, the compiler reads all the code. The closer I can think of something hidden from the compiler are comments, which the compiler ignores, but sometimes contain information for other tools. Addendum: perhaps you mean something like the compiler inlining all private methods or not generating metadata for them. Well, some compilers for some languages would do things like that. Please note that none of what I said here is what we mean by encapsulation.
    – Theraot
    Mar 17, 2021 at 13:03
  • 5
    Encapsulation happens at design time. Mar 17, 2021 at 18:33

3 Answers 3


Encapsulation is a design principle. It's about writing software, has nothing to do with running or compiling software.

To address the two concrete questions:

  1. For the main implementation of Python (CPython), there is no "compile time", because it isn't compiled.

    Python methods are really just data structures. When the interpreter is reading through a source file and runs into a method, it just creates a new entry in the relevant Class' method table, which maps method names to the method implementation. The definition of a double underscored method just declares this entry in a different way, so that it can't be overloaded by a subclass in the regular way.

  2. In Java, the private keyword is just used to communicate to the compiler. Once the compiler learns that you want a method to be private, it'll enforce a certain set of rules around its usage. This metadata does also end up encoded into the byte code, so reflection mechanisms can that the method is private.


Encapsulation is a multi-layered concept. On some lower level it can be interpreted as something having to do with "private" modifiers and not having access to some variable at runtime (so short answer: both). This interpretation is somewhat useless however.

To help with complexity and maintainability, encapsulation needs to be interpreted at a much higher level. What I mean is that encapsulation means to encapsulate knowledge. Knowledge in one object is encapsulated if it does not need to be known anywhere else.

For example data records (beans, DTOs, VOs, etc.) don't encapsulate anything, because there is no (business-related) knowledge they exclusively possess. But even proper objects do need to expect some knowledge that the caller possesses, so it's never "perfect".

So long answer: Encapsulation is conceptual, it's in our heads, not necessarily in code.


The short answer is yes, both. This is language dependent.

For instance, Ruby is object-oriented and interpreted -- there is no compilation step (modulo JIT compilers). Smalltalk is also interpreted (at least originally). C++, Modula, Simula, and Java on the other hand are all compiled.

All of these are OOP languages. So if we take encapsulation as an essential characteristic of OOP (and by extension, OOP languages), by observation it must be divorced from compilation.

  • "So if we take encapsulation as an essential characteristic of OOP" It's not even really an "essential characteristic of OOP". Ironically, C does it best (opaque objects are opaque... you can't even see their private variables), unlike e.g. Java or Ruby where you can trivially reflect to see private members, and even call them.
    – Alexander
    Mar 17, 2021 at 18:11
  • Yeah, that's fair enough. Fundamentally, I think encapsulation, polymorphism and all the rest are really orthogonal concerns to object orientation. I tend to view object orientation as a modeling paradigm vice a code construction paradigm. But we seem to lump them all together and it's in that sense I mean "essential characteristic." Mar 17, 2021 at 18:32
  • Yeah, it's a common (IMO) misconception that trigger my well ackchuyually reflex. Out of the three main traits attributed to OO (encapsulation, inheritance and polymorphism), encapsulation is worse (compare to opaque pointers in C. or returning a closure in a functional programming language, you can't look at the local vars inside even if you wanted), inheritance is "eh". Only polymorphism significantly improved. Bob martin talks about it here: youtube.com/watch?v=t86v3N4OshQ
    – Alexander
    Mar 17, 2021 at 19:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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