# 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

* Wikipedia: Property (programming)
* Wikipedia: Mutator method
* Wikipedia: Constructor (object-oriented programming)

## Multimedia
#### 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

* Temperature
* + celsius: float + fahrenheit: float
* + Temperature(celsius: float = None,     fahrenheit: float = None) + to_celsius(fahrenheit: float) + to_fahrenheit(celsius: float)
* C#
* Java
* JavaScript
* Python3

## Activities

* BMI_Calculator
* + bmi: float {read only} + feet: float + inches: float + kilograms: float + meters: float + pounds: float
* + BMI(feet: float = 0, inches: float = 0,     kilograms: float = 0, meters: float = 0,     pounds: float = 0) + calculate_metric(kilograms: float, meters: float) + calculate_us(pounds: float, feet: float, inches: float) - feet_to_inches(feet: float) - inches_to_meters(inches: float) - kilograms_to_pounds(kilograms: float) - meters_to_inches(meters: float) - pounds_to_kilograms(pounds: float)
* 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.
* Update the main program to use class properties instead of calculate methods.
* 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.

## See Also
#### 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