Object-Oriented Programming/Methods
Appearance
This lesson introduces object methods and related code documentation.
Objectives and Skills
[edit | edit source]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
Readings
[edit | edit source]- Wikipedia: Method (computer programming)
- Wikipedia: Unified Modeling Language
- Wikipedia: Function overloading
Multimedia
[edit | edit source]- YouTube: Classes and Objects Basics
- YouTube: Classes in Python OOP
- YouTube: Java Methods
- YouTube: Unified Modeling Language Tutorial
- YouTube: UML - Unified Modeling Language
- YouTube: Encapsulation in Java - Learn Encapsulation
- YouTube: Creating Properties in C#
Examples
[edit | edit source]Temperature |
---|
+ to_celsius(fahrenheit: float) + to_fahrenheit(celsius: float) |
Activities
[edit | edit source]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) |
- 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.
- 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.
- 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.
- 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
[edit | edit source]- 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:
- 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
[edit | edit source]- 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]
- method overloading
- 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]
See Also
[edit | edit source]- Computer Programming/Functions
- Applied Programming/Functions
- Python Programming/Classes
- Tutorials Teacher: Private and Protected Access Modifiers in Python
- IBM: UML Basics
References
[edit | edit source]- ↑ Microsoft: Software Development Fundamentals Exam Details
- ↑ Brilliant: Methods (OOP)
- ↑ Wikipedia: Method (computer programming)
- ↑ Wikipedia: Method (computer programming)
- ↑ Program Creek: Overriding and Overloading in Java with Examples
- ↑ Wikipedia: Method (computer programming)
- ↑ Wikipedia: Method (computer programming)
- ↑ Wikipedia: Method (computer programming)
- ↑ Wikipedia: Method (computer programming)
- ↑ Wikipedia: Method (computer programming)
- ↑ Wikipedia: Method (computer programming)
- ↑ Wikipedia: Access modifiers
- ↑ Wikipedia: Method (computer programming)
- ↑ Wikipedia: Method (computer programming)
- ↑ Wikipedia: Method (computer programming)
- ↑ Wikipedia: Method (computer programming)
- ↑ Wikipedia: Method (computer programming)
- ↑ Wikipedia: Method (computer programming)
- ↑ Wikipedia: Method (computer programming)
- ↑ Wikipedia: Unified Modeling Language
- ↑ Wikipedia: Unified Modeling Language
- ↑ Wikipedia: Unified Modeling Language
- ↑ Wikipedia: Unified Modeling Language
- ↑ Wikipedia: Unified Modeling Language
- ↑ Wikipedia: Unified Modeling Language
- ↑ Wikipedia: Unified Modeling Language
- ↑ Wikipedia: Class (computer programming)
- ↑ Wikipedia: Method (computer programming)
- ↑ Wikipedia: Method (computer programming)
- ↑ Wikipedia: Method (computer programming)
- ↑ Wikipedia: Unified Modeling Language
- ↑ "Initializing a variable". www.inf.unibz.it. Retrieved 2020-09-01.
- ↑ Wikipedia: Method (computer programming)
- ↑ https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/void#:~:text=You%20use%20void%20as%20the,doesn't%20return%20a%20value.&text=You%20can%20also%20use%20void,pointer%20to%20an%20unknown%20type.&text=You%20cannot%20use%20void%20as%20the%20type%20of%20a%20variable.
- ↑ Wikipedia: Method (computer programming)
- ↑ Wikipedia: Modeling language
- ↑ Wikipedia: Unified Modeling Language
- ↑ Belderbos, Bob. "Enriching Your Python Classes With Dunder (Magic, Special) Methods". dbader.org. Retrieved 2020-09-02.
- ↑ Wikipedia: Unified Modeling Language
- ↑ Wikipedia: Unified Modeling Language