Object-Oriented Programming/Properties

This lesson introduces object properties, attributes, and fields.

Objectives and Skills edit

Objectives and skills for this lesson include:[1]

  • Understand the fundamentals of classes
    • Understand properties, methods, events, and constructors
    • How to create a class
    • How to use classes in code

Readings edit

  1. Wikipedia: Property (programming)
  2. Wikipedia: Mutator method
  3. Wikipedia: Constructor (object-oriented programming)

Multimedia edit

C# edit

JavaScript edit

Python edit

Examples edit

Temperature
+ celsius: float
+ fahrenheit: float
+ Temperature(celsius: float = None,
    fahrenheit: float = None)
+ to_celsius(fahrenheit: float)
+ to_fahrenheit(celsius: float)

Activities edit

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)
  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 edit

  • 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".[2]
  • 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.[3]
  • 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.[4]
  • 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.[5]
  • A mutator method is a method used to control changes to a variable. They are also widely known as setter methods.[6]
  • Often, a setter is accompanied by a getter—also known as an accessor—which returns the value of the private member variable.[7]
  • 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.[8]
  • 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. [9]

Key Terms edit

constructor
A special method used to initialize the data members of a newly created object (instance of a class).[10]
getter
Also known as an accessor, a getter is a method used to return the value of the private member variable.[11]
property
A type of class member that can expose fields.[12]
read-only field
A field that is capable of being read but not written.[13]
setter
Also known as a mutator method, a setter is a method used to control changes to a variable.[14]

See Also edit

C# edit

JavaScript edit

Python edit

References edit

  1. Microsoft: Software Development Fundamentals Exam Details
  2. Wikipedia: Property (programming)
  3. "Python | property() function". GeeksforGeeks. 2018-09-21. Retrieved 2020-09-10.
  4. Wikipedia: Property (programming)
  5. Wikipedia: Property (programming)
  6. Wikipedia: Mutator method
  7. Wikipedia: Mutator method
  8. Wikipedia: Mutator method
  9. Wikipedia: Constructor (object-oriented programming)
  10. Wikipedia: Constructor (object-oriented programming)
  11. Wikipedia: Property (programming)
  12. Wikipedia: Property (programming)
  13. Wikipedia: Property (programming)
  14. Wikipedia: Property (programming)