Object-Oriented Programming/Properties

This lesson introduces object properties, attributes, and fields.

Objectives and Skills
Objectives and skills for this lesson include:
 * Understand the fundamentals of classes
 * Understand properties, methods, events, and constructors
 * How to create a class
 * How to use classes in code

Readings

 * 1)  Property (programming)
 * 2)  Mutator method
 * 3)  Constructor (object-oriented programming)

C#

 * YouTube: C# Programming Tutorial 77 - Optional Parameters

JavaScript

 * YouTube: JavaScript Object Property Descriptors

Python

 * YouTube: Introduction to Python 3 property function and @property decorator
 * YouTube: Python OOP Tutorial 6: Property Decorators - Getters, Setters, and Deleters
 * YouTube: Python Constructors, __init__, self, Class and Instance Objects Explained Concisely

Examples

 * C#
 * /Java/
 * /JavaScript/
 * /Python3/

Activities

 * 1) Extend the BMI class from the previous lesson. Add properties for BMI, feet, inches, kilograms, meters, and pounds. If your language supports it, add a constructor that uses optional parameters for feet, inches, kilograms, meters, and/or pounds as named parameters. The BMI property should be read-only.
 * 2) Update the main program to use class properties instead of calculate methods.
 * 3) Avoid global variables by passing parameters and returning results. Include program, class, and method documentation consistent with the documentation standards for your selected programming language.

Lesson Summary

 * A property, in some object-oriented programming languages, is a special sort of class member, intermediate in functionality between a field (or data member) and a method. The syntax for reading and writing of properties is like for fields, but property reads and writes are (usually) translated to 'getter' and 'setter' method calls. The field-like syntax is easier to read and write than many method calls, yet the interposition of method calls "under the hood" allows for data validation, active updating (e.g., of GUI elements), or implementation of what may be called "read-only fields".


 * By using property method, we can modify our class and implement the value constraint without any change required to the client code. So that the implementation is backward compatible.


 * Some object-oriented languages, such as Java and C++, don't support properties, and require the programmer to define a pair of accessor and mutator methods instead.


 * In most 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.


 * A mutator method is a method used to control changes to a variable. They are also widely known as setter methods.


 * Often, a setter is accompanied by a getter—also known as an accessor—which returns the value of the private member variable.


 * Member variables of a class are made private to hide and protect them from other code, and can only be modified by a public member function (the mutator method), which takes the desired new value as a parameter, optionally validates it, and modifies the private member variable.


 * A constructor (abbreviated: ctor) is a special type of subroutine or method that is used to initialize newly created objects of a class or struct, often accepting arguments which the constructor uses to set member variables or attributes. There are several types of constructors which vary in function and also by language, some of which include: default constructors, copy constructors, parameterized constructors, etc.

Key Terms

 * constructor
 * A special method used to initialize the data members of a newly created object (instance of a class).


 * getter
 * Also known as an accessor, a getter is a method used to return the value of the private member variable.


 * property
 * A type of class member that can expose fields.


 * read-only field
 * A field that is capable of being read but not written.


 * setter
 * Also known as a mutator method, a setter is a method used to control changes to a variable.

C#

 * Geeks for Geeks: C# Optional Parameters

JavaScript

 * W3Schools: JavaScript Properties
 * Typescript: Decorators

Python

 * Programiz: Python @property: How to Use it and Why?
 * Machine Learning Plus: Python @property Simple Explanation and Use-Case Example
 * Python-Course: Properties vs. Getters and Setters