Object-Oriented Programming/Methods

This lesson introduces object methods and related code documentation.

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

Readings

 * 1)  Method (computer programming)
 * 2)  Unified Modeling Language
 * 3)  Function overloading

Multimedia

 * 1) YouTube: Classes and Objects Basics
 * 2) YouTube: Classes in Python OOP
 * 3) YouTube: Java Methods
 * 4) YouTube: Unified Modeling Language Tutorial
 * 5) YouTube: UML - Unified Modeling Language
 * 6) YouTube: Encapsulation in Java - Learn Encapsulation
 * 7) YouTube: Creating Properties in C#

Examples

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

Activities

 * 1) Review  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.


 * In class-based programming, methods are defined in a class.


 * Method overriding is when a subclass redefines the implementation of a method of its superclass.


 * Method overloading is when two or more methods have the same name but different parameters.


 * Accessor (getter) methods are used to read data values of an object.


 * Mutator (setter) methods are used to modify the data of an object.


 * Manager (initialize) methods are used to initialize and destroy objects of a class, e.g. constructors and destructors.


 * A constructor is a method that is called at the beginning of an object's lifetime to create and initialize the object.


 * A destructor is a method that is called automatically at the end of an object's lifetime.


 * A finalizer has a similar purpose and function to destructors, but for garbage-collected languages, such as Java, C#, and Python.


 * Access specifiers (or access modifiers) control the accessibility of classes, methods, and other data members. (ex: public, private, protected, etc.)


 * An abstract method is one with only a signature and no implementation body.


 * Class methods are methods that are called on a class rather than an instance.


 * Special methods are very language-specific and a language may support none, some, or all of the special methods defined here.


 * Static methods are meant to be relevant to all the instances of a class rather than to any specific instance.


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


 * Operator methods define or redefine operator symbols and define the operations to be performed with the symbol and the associated method parameters.


 * Virtual functions are the means by which a class can achieve polymorphic behavior.


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


 * UML is not a development method by itself, but it was made to be compatible with object-oriented software development methods of its time.


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


 * 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.
 * Dynamic (behavioral) view: emphasizes the dynamic behavior of the system by showing collaborations among objects and changes to the internal states of objects.


 * UML can be exchanged among UML tools by using the XML Metadata Interchange (XMI) format.


 * Interaction diagrams are a subset of behavior diagrams which flows up the control and data among the things in the system being modeled.

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.


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


 * constructor
 * A method that creates, initializes, and allocates memory for an object automatically as soon as the object is defined.


 * destructor
 * A method that is called automatically at the end of an object's lifetime that deallocates the memory for the object.


 * diagram
 * A partial graphic representation of a system’s model.


 * initialization
 * Specifying an initial value to assign to a variable.


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


 * void
 * A keyword in C# and other languages used to signal that a method does not return anything.


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


 * modeling language
 * A structured artificial language that is used to represent various kinds of information, such as EXPRESS or Flowchart.


 * MOF
 * Meta-Object Facility is a metamodeling architecture to define UML.


 * special method
 * A set a predefined methods typically used in classes


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


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