Programming languages that support properties include ActionScript 3, C#, D, Delphi/Free Pascal, eC, F#, Kotlin, JavaScript, Objective-C 2.0, Python, Scala, Swift, Lua, and Visual Basic.
Some object-oriented languages, such as Java and C++, do not support properties, requiring the programmer to define a pair of accessor and mutator methods instead.1
Oberon-2 provides an alternative mechanism using object variable visibility flags.
Other languages designed for the Java Virtual Machine, such as Groovy, natively support properties.
While C++ does not have first class properties, they can be emulated with operator overloading.2
Also note that some C++ compilers support first class properties as language extensions.
In many object oriented languages properties are implemented as a pair of accessor/mutator methods, but accessed using the same syntax as for public fields. Omitting a method from the pair yields a read-only or an uncommon write-only property.
In some languages with no built-in support for properties, a similar construct can be implemented as a single method that either returns or changes the underlying data, depending on the context of its invocation. Such techniques are used e.g. in Perl.
Some languages (Ruby, Smalltalk) achieve property-like syntax using normal methods, sometimes with a limited amount of syntactic sugar.
Some languages follow well-established syntax conventions for formally specifying and utilizing properties and methods.
Among these conventions:
The following example demonstrates dot notation in JavaScript.
The following example demonstrates bracket notation in JavaScript.
Recent C# versions also allow "auto-implemented properties" where the backing field for the property is generated by the compiler during compilation. This means that the property must have a setter. However, it can be private.
C++ does not have first class properties, but there exist several ways to emulate properties to a limited degree. Two of which follow:
Also see Stack Overflow for a more detailed example.
An example taken from the MSDN documentation page.
In D version 2, each property accessor or mutator must be marked with @property:
The above example could be used in an arbitrary method like this:
Properties only work correctly for new-style classes (classes that have object as a superclass), and are only available in Python 2.2 and newer (see the relevant section of the tutorial Unifying types and classes in Python 2.2). Python 2.6 added a new syntax involving decorators for defining properties.
Ruby also provides automatic getter/setter synthesizers defined as instance methods of Class.
"Accessors And Mutators In Java". C# Corner - Community of Software and Data Developers. Retrieved 5 January 2022. https://www.c-sharpcorner.com/UploadFile/3614a6/accessors-and-mutators-in-java/ ↩
"Portability of Native C++ properties". Stack Overflow. Stack Overflow. Retrieved 5 January 2022. https://stackoverflow.com/questions/5772480/portability-of-native-c-properties ↩
"property (C++)". Microsoft technical documentation. Microsoft. Retrieved 5 January 2022. https://docs.microsoft.com/en-us/cpp/cpp/property-cpp?view=msvc-170 ↩
"clang::MSPropertyDecl Class Reference". Clang: a C language family frontend for LLVM. Retrieved 5 January 2022. https://clang.llvm.org/doxygen/classclang_1_1MSPropertyDecl.html ↩
"__property Keyword Extension". Embarcadero/IDERA Documentation Wiki. Retrieved 5 January 2022. https://docwiki.embarcadero.com/RADStudio/Sydney/en/Property ↩