Applied Programming/Modules and Classes
This lesson introduces modules and classes.
Objectives and Skills
editObjectives and skills for this lesson include:
- Creating classes and modules
- Managing git branches
- building familiarity with Object-oriented Programming(OOP)
Readings
editMultimedia
edit- YouTube: Python Classes
- YouTube: Classes and Instances
- YouTube: Using Methods with Classes
- YouTube: Understanding OOP vs. Functional Programming
- YouTube: Classes in JavaScript
- YouTube: Python getter and setter Methods
- YouTube: Method Types in Python OOP: @classmethod, @staticmethod, and Instance Methods
- YouTube: Classes and Objects - Basics
- YouTube: Classes and Objects
Examples
editActivities
edit- 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.
- 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.
- 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.
Lesson Summary
edit- 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]
- 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 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]
- 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]
Key Terms
edit- 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
editReferences
edit- ↑ 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 Wikipedia: Modular programming
- ↑ 2.00 2.01 2.02 2.03 2.04 2.05 2.06 2.07 2.08 2.09 2.10 2.11 2.12 2.13 2.14 2.15 2.16 2.17 2.18 2.19 2.20 2.21 2.22 2.23 2.24 2.25 2.26 2.27 Wikipedia: Object-oriented programming
- ↑ 3.00 3.01 3.02 3.03 3.04 3.05 3.06 3.07 3.08 3.09 3.10 3.11 3.12 3.13 3.14 3.15 3.16 3.17 3.18 3.19 3.20 3.21 Wikipedia: Class diagram
- ↑ "Library (computing)". Wikipedia. 2018-06-05. https://en.wikipedia.org/w/index.php?title=Library_(computing)&oldid=844509652.
- ↑ "Pointer (computer programming)". Wikipedia. 2018-07-07. https://en.wikipedia.org/w/index.php?title=Pointer_(computer_programming)&oldid=849208568.
- ↑ 6.0 6.1 Wikipedia: Access modifiers
- ↑ Wikipedia: Procedural programming
- ↑ Wikipedia: State diagram
- ↑ Wikipedia: Unified Modeling Language