Programming Fundamentals/Objects

This lesson introduces objects and object-oriented programming.

Objectives and Skills

edit

Objectives and skills for this lesson include:

Readings

edit
  1. Pressbooks: Programming Fundamentals
  2. Wikipedia: Object-oriented programming

Multimedia

edit
  1. YouTube: Object-oriented Programming in 7 minutes
  2. YouTube: Programming For Beginners - Object Orientation
  3. YouTube: Principles of OOP

Examples

edit

Activities

edit
  1. Review MathsIsFun: Area of Plane Shapes. Create a program that asks the user what shape they would like to calculate the area for. Use if/else conditional statements to determine their selection and then gather the appropriate input and calculate and display the area of the shape. Perform all area calculations using a ShapeArea class that has separate methods to calculate and return the area for different shapes. Include data validation in the class and error handling in the main program.
  2. Create a program that asks the user how old they are in years. Then ask the user if they would like to know how old they are in months, days, hours, or seconds. Use if/else conditional statements to display their approximate age in the selected timeframe. Perform all calculations using an AgeConverter class that accepts the age in years during initialization and has separate properties and methods to calculate and return the age in months, days, hours, and seconds. Include data validation in the class and error handling in the main program.
  3. Review Wikipedia: Zeller's congruence. Create a program that asks the user for their birthday (year, month, and day) and then calculate and display the day of the week on which they were born. Use if/else conditional statements to convert the numeric day of the week to the correct string representation (Monday, Tuesday, Wednesday, etc.). Perform all calculations using a DayOfWeek class that accepts the year, month, and day during initialization and has separate properties and methods to calculate and return the day of week as a number, as an abbreviated string (Mon, Tue, etc.), and as a full string (Monday, Tuesday, etc.). Include data validation in the class and error handling in the main program.

Lesson Summary

edit
  • Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which may contain data, in the form of fields, often known as attributes; and code, in the form of procedures, often known as methods.[1]
  • A feature of objects is that an object's procedures can access and often modify the data fields of the object with which they are associated.[2]
  • Most object-oriented programming languages are class-based, meaning that objects are instances of classes, which typically also determine their type.[3]
  • Classes define the data format and available procedures for a given type of object.[4]
  • Classes may contain data members and member functions (known as class methods) themselves.[5]
  • Objects are instances of classes.[6]
  • Each object is said to be an instance of a particular class.[7]
  • Procedures in object-oriented programming are known as methods; variables are also known as fields, members, attributes, or properties.[8]
  • Class variables belong to the class as a whole; there is only one copy of each one..[9]
  • Instance variables or attributes belong to individual objects; every object has its own copy of each one.[10]
  • Member variables refer to both the class and instance variables that are defined by a particular class.[11]
  • Class methods belong to the class as a whole and have access only to class variables and inputs from the procedure call.[12]
  • Instance methods belong to individual objects, and have access to instance variables for the specific object they are called on, inputs, and class variables.[13]
  • Objects provide a layer of abstraction which can be used to separate internal from external code.[14]
  • Objects are created by calling a special type of method in the class known as a constructor.[15]
  • Encapsulation is an object-oriented programming concept that binds together the data and functions that manipulate the data, and that keeps both safe from outside interference and misuse. Data encapsulation supports data or information hiding.[16]
  • Some languages let classes enforce access restrictions explicitly, for example denoting internal data with the private keyword and designating methods intended for use by code outside the class with the public keyword.[17]

Key Terms

edit
abstraction
A technique for arranging complexity of computer systems so that functionality may be separated from specific implementation details.[18][19]
accessor
A method used to return the value of a private member variable, also known as a getter method.[20]
attribute
A specification that defines a property of an object.[21]
class
An extensible program-code-template for creating objects, providing initial values for state (member variables) and implementations of behavior (member functions or methods).[22]
encapsulation
A language mechanism for restricting direct access to some of an object's components.[23]
field
Also known as data member or member variable, is data encapsulated within a class or object.[24]
information hiding
The principle of segregation of the design decisions in a computer program from other parts of the program. See encapsulation.[25]
inheritance
An object or class being based on another object or class, using the same implementation or specifying a new implementation to maintain the same behavior.[26]
instance
A concrete occurrence of an object.[27]
method
A specification that defines a procedure or behavior of an object.[28]
mutator
A method used to control changes to a private member variable, also known as a setter method.[29]
object
A particular instance of a class where the object can be a combination of variables, functions, and data structures.[30]
polymorphism
The provision of a single interface to entities of different types.[31]
private
An access modifier that restricts visibility of a property or method to the class in which it is defined.[32]
property
A class member, intermediate in functionality between a field (or data member) and a method, which supports reading and writing like a field using 'getter' and 'setter' method calls.[33]
public
An access modifier that opens visibility of a property or method to all other classes.[34]
state
Recorded information supporting preceding events or user interactions.[35]
static
Properties or methods of a class that apply to all instances of the class rather than to any specific instance.[36]
this, self, or Me
Keywords used in some computer programming languages to refer to the object, class, or other entity that the currently running code is part of.[37]

Assessments

edit

See Also

edit

References

edit
  1. Wikipedia: Object-oriented programming
  2. Wikipedia: Object-oriented programming
  3. Wikipedia: Object-oriented programming
  4. Wikipedia: Object-oriented programming
  5. Wikipedia: Object-oriented programming
  6. Wikipedia: Object-oriented programming
  7. Wikipedia: Object-oriented programming
  8. Wikipedia: Object-oriented programming
  9. Wikipedia: Object-oriented programming
  10. Wikipedia: Object-oriented programming
  11. Wikipedia: Object-oriented programming
  12. Wikipedia: Object-oriented programming
  13. Wikipedia: Object-oriented programming
  14. Wikipedia: Object-oriented programming
  15. Wikipedia: Object-oriented programming
  16. Wikipedia: Object-oriented programming
  17. Wikipedia: Object-oriented programming
  18. Wikipedia: Object-oriented programming
  19. Wikipedia: Abstraction (software engineering)
  20. Wikipedia: Mutator method
  21. Wikipedia: Attribute (computing)
  22. Wikipedia: Class (computer programming)
  23. Wikipedia: Encapsulation (computer programming)
  24. Wikipedia: Field (computer science)
  25. Wikipedia: Information hiding
  26. Wikipedia: Inheritance (object-oriented programming)
  27. Wikipedia: Instance (computer science)
  28. Wikipedia: Method (computer programming)
  29. Wikipedia: Mutator method
  30. Wikipedia: Object (computer science)
  31. Wikipedia: Polymorphism (computer science)
  32. Wikipedia: Access modifiers
  33. Wikipedia: Property (programming)
  34. Wikipedia: Access modifiers
  35. Wikipedia: State (computer science)
  36. Wikipedia: Method (computer programming)
  37. Wikipedia: this (computer programming)