23.3: Objects and Classes
-
- Last updated
- Save as PDF
Overview
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. 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 (objects have a notion of “this” or “self”). There is significant diversity of OOP languages, but the most popular ones are class-based, meaning that objects are instances of classes, which typically also determine their type. [1]
Discussion
Thus far, we have focused on procedural programming. Based on structured programming, procedures (routines, subroutines, or functions) contain a series of computational steps to be carried out. Any given procedure might be called at any point during a program’s execution, including by other procedures or itself. The focus of procedural programming is to break down a programming task into a collection of variables, data structures, and subroutines. [2] Small programs and scripts tend to be easier to develop using a simple procedural approach.
Object-oriented programming instead breaks down a programming task into objects that expose behavior (methods) and data (members or attributes) using interfaces. The most important distinction is that while procedural programming uses procedures to operate on separate data structures, object-oriented programming bundles the two together, so an “object”, which is an instance of a class, operates on its “own” data structure. [3] Larger programs benefit from better code and data isolation and reuse provided by an object-oriented approach.
Objects and classes are often designed to represent real-world objects. Consider a door as an example of a real-world object. Most doors have limited functionality. They may be opened and closed, and locked and unlocked. In procedural programming, we might design functions to open, close, lock, and unlock a door, such as:
Procedural Programming - Functions OpenDoor(door) CloseDoor(door) LockDoor(door) UnlockDoor(door)
Object-oriented programming combines code and data, so that, rather than having separate functions act on doors, we design doors that have methods that can act on themselves. Methods represent something the object can do, and are typically defined using verbs. Object-oriented door pseudocode might look like:
Object-Oriented Programming - Methods door.Open() door.Close() door.Lock() door.Unlock()
Objects may also have attributes, something the object is or has, and are typically defined using nouns or adjectives. Door attributes might include:
Object-Oriented Programming - Attributes door.Height door.Width door.Color door.Closed door.Locked
When we write code to define a generic door, we would create a door class. The door class would contain all of the methods a door can perform and all of the attributes a door might have. We would then create instances of the class (objects) to represent specific doors, such as a front door, back door, or room door on a house, or a left door and right door on a car.
Key Terms
- attribute
- A specification that defines a property of an object. [4]
- class
- An extensible program-code-template for creating objects, providing initial values for state (member variables) and implementations of behavior (member functions or methods). [5]
- instance
- :A concrete occurrence of an object. [6]
- method
- A specification that defines a procedure or behavior of an object. [7]
- object
- A particular instance of a class where the object can be a combination of variables, functions, and data structures. [8]
- 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. [9]
References
- Wikibooks: Object-Oriented Programming
- Wikipedia: Object-oriented programming
- Wikiversity: Computer Programming
-
Wikipedia: Object-oriented programming
↵
-
Wikipedia: Object-oriented programming
↵
-
Wikipedia: Object-oriented programming
↵
-
Wikipedia: Attribute (computing)
↵
-
Wikipedia: Class (computer programming)
↵
-
Wikipedia: Instance (computer science)
↵
-
Wikipedia: Method (computer programming)
↵
-
Wikipedia: Object (computer science)
↵
-
Wikipedia: this (computer programming)
↵