# Object-Oriented Programming/Methods

This lesson introduces object methods and related code documentation.

## Objectives and Skills

Objectives and skills for this lesson include:[1]

• Understand the fundamentals of classes
• Properties, methods, events, and constructors; how to create a class; how to use classes in code

## Examples

Temperature
+ to_celsius(fahrenheit: float)
+ to_fahrenheit(celsius: float)

## Activities

BMI_Calculator
+ calculate_metric(kilograms: float, meters: float)
+ calculate_us(pounds: float, feet: float, inches: float)
- pounds_to_kilograms(pounds: float)
- feet_to_inches(feet: float)
- inches_to_meters(inches: float)
1. Review Wikipedia: Coding conventions Research style guides or coding standards for your selected programming language, looking specifically for object-oriented class and method documentation. If applicable, discuss coding conventions with your classmates and agree on a standard to follow for this course.
2. Extend the BMI program from the previous lesson. Create a separate class module for the BMI calculator based on the UML diagram provided. Add two public methods for the BMI calculation (metric and US). Include private methods to convert pounds to kilograms, feet to inches, and inches to meters. Perform all calculations inside the class module.
3. Update the main program to create an instance of the BMI calculator class. Perform all input and output in the main program. Ask the user whether they want to enter metric or U.S. units and then obtain corresponding input values. Use the BMI calculator class to process user input (perform calculations). Display results in the main program.
4. Avoid global variables by passing parameters and returning results. Define constants for height and weight conversions and use self-documenting method, variable, and constant names that follow the naming conventions for your selected programming language. Include comments at the top of each source code module and comments for each method that are consistent with the documentation conventions for your selected programming language.

## Lesson Summary

• A method in object-oriented programming is the equivalent of a function in procedural programming.[2]
• In class-based programming, methods are defined in a class.[3]
• Method overriding is when a subclass redefines the implementation of a method of its superclass.[4]
• Method overloading is when two or more methods have the same name but different parameters.[5]
• Accessor (getter) methods are used to read data values of an object.[6]
• Mutator (setter) methods are used to modify the data of an object.[7]
• Manager (initialize) methods are used to initialize and destroy objects of a class, e.g. constructors and destructors.[8]
• A constructor is a method that is called at the beginning of an object's lifetime to create and initialize the object.[9]
• A destructor is a method that is called automatically at the end of an object's lifetime.[10]
• A finalizer has a similar purpose and function to destructors, but for garbage-collected languages, such as Java, C#, and Python.[11]
• Access specifiers (or access modifiers) control the accessibility of classes, methods, and other data members. (ex: public, private, protected, etc.) [12]
• An abstract method is one with only a signature and no implementation body.[13]
• Class methods are methods that are called on a class rather than an instance. [14]
• Special methods are very language-specific and a language may support none, some, or all of the special methods defined here. [15]
• Static methods are meant to be relevant to all the instances of a class rather than to any specific instance.[16]
• Copy-assignment operators define actions to be performed by the compiler when a class object is assigned to a class object of the same type.[17]
• Operator methods define or redefine operator symbols and define the operations to be performed with the symbol and the associated method parameters. [18]
• Virtual functions are the means by which a class can achieve polymorphic behavior.[19]
• UML has a way to show a system’s blueprints in a diagram, including activities (jobs), individual components and how they interact with other software components, how to run the system, how entities interact with others and external user interface.[20]
• UML is not a development method by itself, but it was made to be compatible with object-oriented software development methods of its time.[21]
• A set of diagrams don’t need to cover the model and deleting a diagram doesn’t change the model. The model may also include documentation that drives the model elements and diagrams.[22]
• UML diagrams show two different views of a system model:
• Static (structural) view: shows the static structure of the system by using objects, attributes, operations, and relationships.[23]
• Dynamic (behavioral) view: emphasizes the dynamic behavior of the system by showing collaborations among objects and changes to the internal states of objects.[24]
• UML can be exchanged among UML tools by using the XML Metadata Interchange (XMI) format.[25]
• Interaction diagrams are a subset of behavior diagrams which flows up the control and data among the things in the system being modeled.[26]

## Key Terms

class
The definition of a type of object, including information about the data format. It also defines the data and procedures (class methods) applicable to the object.[27]
class method
A procedure associated with a message and an object defined as part of a class. It only has access to class variables, as well as inputs (parameters) called with the procedure.[28]
constructor
A method that creates, initializes, and allocates memory for an object automatically as soon as the object is defined.[29]
destructor
A method that is called automatically at the end of an object's lifetime that deallocates the memory for the object.[30]
diagram
A partial graphic representation of a system’s model.[31]
initialization
Specifying an initial value to assign to a variable.[32]
Happens when a class has more than one method with the same name, but each method has a different number of arguments. The code will then execute the method with a matching list of parameter types.[33].
void
A keyword in C# and other languages used to signal that a method does not return anything.[34]
method overriding
Happens when a subclass uses a method by the same name as its superclass but redefines what it does specifically for the subclass.[35]
modeling language
A structured artificial language that is used to represent various kinds of information, such as EXPRESS or Flowchart.[36]
MOF
Meta-Object Facility is a metamodeling architecture to define UML.[37]
special method
A set a predefined methods typically used in classes[38]
UML
Unified Modeling Language is a general developmental modeling language which provides a standard way to visualize a design of a system in the software engineering field.[39]
use-case model
One of the key tools for behavior modeling in UML and it is the way for specifying required usages of a system.[40]

## References

1. Microsoft: Software Development Fundamentals Exam Details
2. Brilliant: Methods (OOP)
3. Wikipedia: Method (computer programming)
4. Wikipedia: Method (computer programming)
6. Wikipedia: Method (computer programming)
7. Wikipedia: Method (computer programming)
8. Wikipedia: Method (computer programming)
9. Wikipedia: Method (computer programming)
10. Wikipedia: Method (computer programming)
11. Wikipedia: Method (computer programming)
12. Wikipedia: Access modifiers
13. Wikipedia: Method (computer programming)
14. Wikipedia: Method (computer programming)
15. Wikipedia: Method (computer programming)
16. Wikipedia: Method (computer programming)
17. Wikipedia: Method (computer programming)
18. Wikipedia: Method (computer programming)
19. Wikipedia: Method (computer programming)
20. Wikipedia: Unified Modeling Language
21. Wikipedia: Unified Modeling Language
22. Wikipedia: Unified Modeling Language
23. Wikipedia: Unified Modeling Language
24. Wikipedia: Unified Modeling Language
25. Wikipedia: Unified Modeling Language
26. Wikipedia: Unified Modeling Language
27. Wikipedia: Class (computer programming)
28. Wikipedia: Method (computer programming)
29. Wikipedia: Method (computer programming)
30. Wikipedia: Method (computer programming)
31. Wikipedia: Unified Modeling Language
32. "Initializing a variable". www.inf.unibz.it. Retrieved 2020-09-01.
33. Wikipedia: Method (computer programming)
34. Wikipedia: Method (computer programming)
35. Wikipedia: Modeling language
36. Wikipedia: Unified Modeling Language
37. Belderbos, Bob. "Enriching Your Python Classes With Dunder (Magic, Special) Methods". dbader.org. Retrieved 2020-09-02.
38. Wikipedia: Unified Modeling Language
39. Wikipedia: Unified Modeling Language