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


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


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


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


+ 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]
  • A constructor is a method that is called at the beginning of an object's lifetime to create and initialize the object.[8]
  • A destructor is a method that is called automatically at the end of an object's lifetime.[9]
  • A finalizer has a similar purpose and function to destructors, but for garbage-collected languages, such as Java, C#, and Python.[10]
  • An abstract method is one with only a signature and no implementation body.[11]
  • Class methods are methods that are called on a class rather than an instance. [12]
  • Static methods are meant to be relevant to all the instances of a class rather than to any specific instance.[13]
  • 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.[14]
  • Virtual functions are the means by which a class can achieve polymorphic behavior.[15]
  • 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.[16]
  • UML is not a development method by itself, but it was made to be compatible with object-oriented software development methods of its time.[17]
  • 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.[18]
  • 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.[19]
    • Dynamic (behavioral) view: emphasizes the dynamic behavior of the system by showing collaborations among objects and changes to the internal states of objects.[20]
  • UML can be exchanged among UML tools by using the XML Metadata Interchange (XMI) format.[21]
  • Interaction diagrams are a subset of behavior diagrams which flows up the control and data among the things in the system being modeled.[22]

Key Terms[edit]

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.[23]
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.[24]
A method that creates, initializes, and allocates memory for an object automatically as soon as the object is defined.[25]
A method that is called automatically at the end of an object's lifetime that deallocates the memory for the object.[26]
A partial graphic representation of a system’s model.[27]
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.[28]
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.[29]
modeling language
A structured artificial language that is used to represent various kinds of information, such as EXPRESS or Flowchart.[30]
Meta-Object Facility is a metamodeling architecture to define UML.[31]
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.[32]
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.[33]

See Also[edit]