Public, Private, Protected and Published

By Erick Engelke
Aug 11, 2024

There are four possible class sections in EWB’s Object Pascal, and using them can be confusing until you get the hang of things.

Quoting from EWB’s web site:

Table 1. Class Sections
Type Description

Private

Any member variables, properties, or methods declared in this scope are only visible to the properties or methods of the same class declaration.

Protected

Any member variables, properties, or methods declared in this scope are only visible to the properties or methods of the same class declaration, or any descendant class declarations.

Public

Any member variables, properties, or methods declared in this scope are visible everywhere, subject to the scoping rules of the referencing declaration or code block.

Published

Same as Public, but in addition, all properties declared in this scope are streamable and will appear in the IDE’s component inspector.

Class Sections

Beginners tend to put everything into Public, which means you can access the content from other code. That’s like declaring all your variables as global - sure it works, but it doesn’t scale well into larger projects. It’s not a best practice.

The more experienced developers will put almost everything into Private - thereby hiding the content and controlling all access through only a few public or published items.

Protected is when you really wnat things to be private or at least not public, but wish to share them with other classes that inherit from yours.

Published is how you get fields into the Object inspector if you dd them as new components.

Transpiler Magic

In reality, the EWB to JavaScript transpiler (compiler to a different language) does make all class variables and functions available to all code globaally, it’s just that the Object Pascal rules prevent you from seeing them at compile time and thus help you maintain safety of data.

JavaScript has global visibility. So EWB prepends the unit and class name to things to prevent name conflicts and thus separate variables and classe elements.

I encourge you to look at the generated JavaScript and see how your classes look to the browser.

Class Cracking in Same Unit

EWB 3 supports class cracking that allows you to access methods from other classes when in the same Object Pascal unit. I wrote a complete blog article on that subject here.

External Classes

EWB treats External classes differently. In particular:

  • they honour uppercase and lowercase. Otherwise ObjectPascal is case insensitive.

  • they do not prepend anything onto the variable or method names in the generated javascript

  • properties are marked read and/or write accessible, which is a convention of the transpiler, not a feature of the JavaScript interpreter.

Conclusion

A standard rule of systems development is that you expose only the bare minimum of interfaces (funcitons and varariables) between different sections of code. That encourages well defined boundaries, which means code in lower sections can be more easily be modified without breaking the higher level sections (or at least not as often as if you didn’t observe these rules).