Object-Oriented Programming/Methods

From Wikiversity
Jump to navigation Jump to search

This lesson introduces object methods and related code documentation.

Objectives and Skills[edit]

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]

  1. Wikipedia: Method (computer programming)
  2. Wikipedia: Unified Modeling Language

Multimedia[edit]

  1. YouTube: Classes and Objects Basics
  2. YouTube: Classes in Python OOP
  3. YouTube: Java Methods
  4. YouTube: Unified Modeling Language Tutorial

Examples[edit]

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

Activities[edit]

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 class for the BMI calculator and 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. 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. 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[edit]

  • 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]
  • An abstract method is one with only a signature and no implementation body.[12]
  • Class methods are methods that are called on a class rather than an instance. [13]
  • Special methods are very language-specific and a language may support none, some, or all of the special methods defined here. [14]
  • Static methods are meant to be relevant to all the instances of a class rather than to any specific instance.[15]
  • 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.[16]
  • Operator methods define or redefine operator symbols and define the operations to be performed with the symbol and the associated method parameters. [17]
  • Virtual functions are the means by which a class can achieve polymorphic behavior.[18]
  • 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.[19]
  • UML is not a development method by itself, but it was made to be compatible with object-oriented software development methods of its time.[20]
  • 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.[21]
  • 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.[22]
    • Dynamic (behavioral) view: emphasizes the dynamic behavior of the system by showing collaborations among objects and changes to the internal states of objects.[23]
  • UML can be exchanged among UML tools by using the XML Metadata Interchange (XMI) format.[24]
  • Interaction diagrams are a subset of behavior diagrams which flows up the control and data among the things in the system being modeled.[25]

Key Terms[edit]

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.[26]
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.[27]
constructor
A method that creates, initializes, and allocates memory for an object automatically as soon as the object is defined.[28]
destructor
A method that is called automatically at the end of an object's lifetime that deallocates the memory for the object.[29]
diagram
A partial graphic representation of a system’s model.[30]
method overloading
Happens when a class has more than one method with the same name, but each method has different arguments. The code will then execute the method with a matching list of parameter types.[31]
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.[32]
modeling language
A structured artificial language that is used to represent various kinds of information, such as EXPRESS or Flowchart.[33]
MOF
Meta-Object Facility is a metamodeling architecture to define UML.[34]
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.[35]
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.[36]

See Also[edit]

References[edit]

  1. Microsoft: Software Development Fundamentals Exam Details
  2. Brilliant: Methods (OOP)
  3. Wikipedia: Method (computer programming)
  4. Wikipedia: Method (computer programming)
  5. Program Creek: Overriding and Overloading in Java with Examples
  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: Method (computer programming)
  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: Unified Modeling Language
  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: Class (computer programming)
  27. Wikipedia: Method (computer programming)
  28. Wikipedia: Method (computer programming)
  29. Wikipedia: Method (computer programming)
  30. Wikipedia: Unified Modeling Language
  31. Wikipedia: Method (computer programming)
  32. Wikipedia: Method (computer programming)
  33. Wikipedia: Modeling language
  34. Wikipedia: Unified Modeling Language
  35. Wikipedia: Unified Modeling Language
  36. Wikipedia: Unified Modeling Language