Applied Programming/Modules and Classes

From Wikiversity
Jump to navigation Jump to search

This lesson introduces modules and classes.

Objectives and Skills[edit | edit source]

Objectives and skills for this lesson include:

  • Creating classes and modules
  • Managing git branches
  • building familiarity with Object-oriented Programming(OOP)

Readings[edit | edit source]

  1. Wikipedia: Modular programming
  2. Wikipedia: Object-oriented programming
  3. Wikipedia: Class diagram

Multimedia[edit | edit source]

  1. YouTube: Python Classes
  2. YouTube: Classes and Instances
  3. YouTube: Using Methods with Classes
  4. YouTube: Understanding OOP vs. Functional Programming
  5. YouTube: Classes in JavaScript
  6. YouTube: Python getter and setter Methods
  7. YouTube: Method Types in Python OOP: @classmethod, @staticmethod, and Instance Methods
  8. YouTube: Classes and Objects - Basics
  9. YouTube: Classes and Objects

Examples[edit | edit source]

Activities[edit | edit source]

  1. Review Gordon.edu: ATM Example Class Diagram. Depending on time available, develop one or more class modules for an ATM based on the diagram provided. If applicable, work together with your classmates and have each person implement a separate module and class.
  2. For each class module, include appropriate data validation and parameter validation. Add module and method documentation, consistent with the documentation standards for your selected programming language.
  3. If you are working as a class, read about git to learn more about team-based programming contribution management and a commonly used version control system.
Read: git - using branches

Lesson Summary[edit | edit source]

  • Modular programming is a software design technique that emphasizes separating the functionality of a program into reusable, independent modules.[1]
    • This concept is related to structured programming and object-oriented programming; all have the same end goal of deconstructing a comprehensive program into smaller pieces.[1]
  • Modular programming refers to high-level decomposition of the code of an entire program into pieces, while structured programming to the low-level code use of structured control flow, and object-oriented programming to the data use of objects, a kind of data structure.[1]
  • Languages that formally support the module concept include Ada, Algol, BlitzMax, C#, Clojure, COBOL, D, Dart, eC, Erlang, Elixir, F, F#, Fortran, Go, Haskell, IBM/360 Assembler, IBM i Control Language (CL), IBM RPG, Java, MATLAB, ML, Modula, Modula-2, Modula-3, Morpho, NEWP, Oberon, Oberon-2, Objective-C, OCaml, several derivatives of Pascal (Component Pascal, Object Pascal, Turbo Pascal, UCSD Pascal), Perl, PL/I, PureBasic, Python, Ruby, Rust, JavaScript, Visual Basic .NET and WebDNA.[1]
  • Modular programming can be performed even where the programming language lacks explicit syntactic features to support named modules, like, for example, in C. This is done by using existing language features, together with, for example, coding conventions, programming idioms and the physical code structure.[1]
  • With modular programming, concerns are separated such that modules perform logically discrete functions, interacting through well-defined interfaces.[1]
  • When creating a modular system, instead of creating a monolithic application (where the smallest component is the whole), several smaller modules are written separately so that, when composed together, they construct the executable application program.[1]
  • This makes modular designed systems, if built correctly, far more reusable than a traditional monolithic design, since all (or many) of these modules may then be reused in other projects. This also facilitates the "breaking down" of projects into several smaller projects.[1]
  • Object-oriented programming is a language model based on the concept of objects. This approach to programming is an evolution of design practices that enabled the reuse of software.[2]
    • The most popular object-oriented languages are class-based, meaning objects are instances of particular classes. The most popular OOP languages include Python, Java, JavaScript, C++, C#, Ruby, PHP, Object Pascal, Objective-C, and Swift.[2]
  • Objects are accessed somewhat like variables with complex internal structure, and in many languages are effectively pointers, serving as actual references to a single instance of said object in memory within a heap or stack.[2]
  • Languages called "pure" OO languages, because everything in them is treated consistently as an object, from primitives such as characters and punctuation, all the way up to whole classes, prototypes, blocks, modules, etc.[2]
    • They were designed specifically to facilitate, even enforce, OO methods. Examples: Python, Ruby, Scala, Smalltalk, Eiffel, Emerald, JADE, Self.[2]
  • Objects can contain other objects in their instance variables; this is known as object composition.[2]
    • For example, an object in the Employee class might contain (either directly or through a pointer) an object in the Address class, in addition to its own instance variables like "first_name" and "position".[2]
  • Object-oriented programming entails the use of classes, a template for object instantiation. An object is a user-created data type that carries its own attributes (fields) and behaviors (methods).[2]
  • Languages that support classes almost always support inheritance. This allows classes to be arranged in a hierarchy that represents "is-a-type-of" relationships. For example, class Employee might inherit from class Person. All the data and methods available to the parent class also appear in the child class with the same names.[2]
  • Both object-oriented programming and relational database management systems (RDBMSs) are extremely common in software today. Since relational databases don't store objects directly (though some RDBMSs have object-oriented features to approximate this), there is a general need to bridge the two worlds.[2]
  • OOP can be used to associate real-world objects and processes with digital counterparts. However, not everyone agrees that OOP facilitates direct real-world mapping or that real-world mapping is even a worthy goal, that a program is not a model of the world but a model of some part of the world.[2]
  • In languages that support open recursion, object methods can call other methods on the same object, including themselves, typically using a special variable or keyword called this or self.[2]
    • This variable is late-bound;[2]
    • It allows a method defined in one class to invoke another method that is defined later, in some subclass thereof.[2]
  • In the design of a system, many classes are grouped together in a class diagram that helps to determine the static relations between them. With detailed modelling, the classes of the conceptual design are often split into a number of subclasses.[3]
  • classes diagrams are represented with three compartments:[3]
    • The top compartment contains the name of the class.[3]
    • The middle compartment contains the attributes of the class.[3]
    • The bottom compartment contains the operations the class can execute.[3]

The UML specifies two types of scope for members: instance and classifier, and the latter is represented by underlined names.[3]

  • Classifier members are commonly recognized as “static” in many programming languages. The scope is the class itself.[3]
    • Attribute values are equal for all instances[3]
    • Method invocation does not affect the instance’s state[3]
    • Instance members are scoped to a specific instance[3]
    • Attribute values may vary between instances[3]
    • Method invocation may affect the instance’s state (i.e. change instance’s attributes) [3]
  • To indicate a classifier scope for a member, its name must be underlined. Otherwise, instance scope is assumed by default.[3]
  • Instance-level relationships consist of:[3]
  • Class-level relationships[3]
    • Generalization/Inheritance[3]
    • Realization/Implementation[3]

Key Terms[edit | edit source]

class
A blueprint for constructing an object—a set of variables and methods that defines an entity.[2]
class diagram
A graphical representation of the structure of an object-oriented system that displays their attributes and relationships.[3]
class methods
belong to the class as a whole and have access only to class variables and inputs from the procedure call.[2]
class variable
A variable that belongs to an entire class; there is only one such variable shared between all objects.[2]
encapsulation
The act of hiding implementation details, either to protect internal data or for the purpose of abstraction.[2]
instance methods
belong to individual objects, and have access to instance variables for the specific object they are called on, inputs, and class variables.[2]
instance variable
A variable that is unique and belongs to each instance of a class.[2]
library
When a program invokes a library, it gains the behavior implemented inside that library without having to implement that behavior itself. Libraries encourage the sharing of code in a modular fashion, and ease the distribution of the code.[4]
me, self, this
A keyword that refers to the current object of focus.[2]
member variable
A variable that is either a class or instance variable.[2]
method
A function that is defined inside a class.[2]
object
A particular instance of a class.[2]
object composition
is that objects can contain other objects in their instance variables.[2]
pointer
A pointer references a location in memory. As an analogy, a page number in a book's index could be considered a pointer to the corresponding page.[5]
private
An access modifier which limits external access.[6]
property
An intermediary between a variable and a method, providing the functionality of both.[2]
public
An access modifier which opens up external access.[6]
Procedural programming
is a programming paradigm, derived from structured programming[citation needed], based upon the concept of the procedure call. Procedures, also known as routines, subroutines, or functions, simply contain a series of computational steps to be carried out.[7]
static
A keyword used in some languages to designate a variable or method as shared between objects.[2]
state diagram
is a type of diagram used in computer science and related fields to describe the behavior of systems.[8]
UML
(Unified Modeling Language) is a picture of an object oriented system.[9]
Variables
Store information formatted in a small number of built-in data types like integers and alphanumeric characters.[2]

See Also[edit | edit source]

References[edit | edit source]