Object-oriented programming (OOP) is a common and powerful programming paradigm that heavily incorporates ideas of abstraction. Abstraction allows programmers to write code that shows the essential features of a piece of software without including the background details. Some common object-oriented programming languages include Python, Java, Ruby, and C++. Object-oriented programming languages often use classes, which group objects, attributes, and methods together for user-friendly and modular programming.
To fully appreciate object-oriented programming, you should be familiar with the basics of programming — check out the see also to see some suggested pages about basic programming.
Here is an excerpt from a 1994 Rolling Stone interview where Apple co-founder Steve Jobs explains what object-oriented programming is.
Jeff Goodell: Would you explain, in simple terms, exactly what object-oriented software is?
Steve Jobs: Objects are like people. They’re living, breathing things that have knowledge inside them about how to do things and have memory inside them so they can remember things. And rather than interacting with them at a very low level, you interact with them at a very high level of abstraction, like we’re doing right here.
Here’s an example: If I’m your laundry object, you can give me your dirty clothes and send me a message that says, “Can you get my clothes laundered, please.” I happen to know where the best laundry place in San Francisco is. And I speak English, and I have dollars in my pockets. So I go out and hail a taxicab and tell the driver to take me to this place in San Francisco. I go get your clothes laundered, I jump back in the cab, I get back here. I give you your clean clothes and say, “Here are your clean clothes.”
You have no idea how I did that. You have no knowledge of the laundry place. Maybe you speak French, and you can’t even hail a taxi. You can’t pay for one, you don’t have dollars in your pocket. Yet I knew how to do all of that. And you didn’t have to know any of it. All that complexity was hidden inside of me, and we were able to interact at a very high level of abstraction. That’s what objects are. They encapsulate complexity, and the interfaces to that complexity are high level.
As Jobs explains, object-oriented programming allows programmers to use code written by other coders without having to know all of the details of the code. This is a vital feature for writing large pieces of software and making software-based products like search engines, apps, social media site, and more. Object-oriented code essentially sets up an agreement between the program and the computer: “If you give me these inputs, I will give you this output.” This allows for easy modular and collaborative programming.
It is good to note that using OOP is entirely optional and most programs can be written without it. A programmer can do a lot just by the use of functions and variables. The primary advantage of using OOP is that it is an extremely useful way of organizing code, especially for very large projects.
Object-oriented programming uses a different approach to programming than functional programming.
There are several features that object-oriented programming depends on.
Variables or Attributes: Attributes are characteristics of an object. These are passed in as arguments to an object. For example, if there is a
Car object, it might have attributes such as
Methods/Procedures: A method is the equivalent of a function in object-oriented programming. For example, the
Car object mentioned above might have a method for changing speed,
Objects are an instance of a class. Objects can interact with one another without having to know all the details of their internal code. More than one instance of the same class can exist at any one time, which means there can be multiple objects made from the same class.
Classes: A blueprint for creating objects (a particular data structure), providing initial values for state (member variables), and implementations of behavior (member functions/methods).
Inheritance: Inheritance describes how the attributes of base cases are inherited by the derived class. A subclass inherits attributes and methods from its parent classes.
Encapsulation: Encapsulation is when attributes and methods are stored in a single class. The process of providing a public interface to interact with the object while hiding other information inside the object. Encapsulation means that the internal representation of an object is generally hidden from view outside of the object's definition.The main way that encapsulation helps reduce rippling effects of change is by keeping as many of the implementation details private to the class. By limiting the interface only to those members needed to use the class, many changes can be made to the implementation without affecting any code that uses the class. The class can be thought of as a 'capsule' or container for data and operations.
Many times, it is easiest to explain object-oriented programming through real world examples. This section will tie together many of the key components of OOP described in the previous section. For a more in-depth, step-by-step explanation of those components, please see the corresponding wiki page.
A quick check in:
Methods and attributes might seem a little strange, but if you have worked with Python lists or strings, you have probably already interacted with methods associated with those data types.
For example to add something to a list in Python,
1 2 3 4
A = [1,2,3] A.append(4) print A #outputs [1,2,3,4]
appendis a method on the list in Python.
In Python, the
datetimemodule contains examples of attributes. A
datetimeobject has several pieces of information associated with it that a user can extract. For example, to get the day from the datetime object, a user could call
datetime.dayand to get the year, the user could call
datetime.yearand so on. The day and the year are attributes of a datetime object.
datetime.datetime.year <attribute 'year' of 'datetime.date' objects>
Let’s look at an example of object-oriented programming regarding superheroes in a video game. In this game, the player controls a superhero with a given set of properties (attributes) and each superhero can perform or be affected by various actions (methods).
In this example, the objects or instances are the individual superheroes described by the class
Superhero. There could be an Ironman object, a Wonder Woman object, a Batman object, or a Spiderman object. The class
Superhero associates certain attributes and methods with the superhero objects. For this example, each superhero will have the following attributes: strength, superpower, costume color, secret identity, points, and health. Additionally, each superhero will have the following methods (or actions): attack and heal.
The class allows the user to make as many hero instances as they want — the user could add a Superman instance with unique attributes that fit Superman, and the Superman character would be able to perform the same actions as the other superhero instances.
Basically, the class sets up a template for making as many instances of the class as the user wants, so that each instance behaves the same way (in that they are the same sort of data type) and has the same characteristics (though possibly with different values) associated with it.
Each hero is initialized with some values in their attributes and as the game progresses, some of these attributes might be affected by methods. For example, if Batman is attacked, the
is_attacked method is applied to him and he loses health.
Here is the Python implementation of this class and how to create superhero objects.
First, the class needs to be initialized, and attributes and methods need to be defined. Then, when the individual instances of the superhero class are created, each hero’s attributes must be initialized.
1 2 3 4 5 6 7 8 9
Here’s how to create the
The programmer can define methods associated with the class. For the
Superhero class, let’s define
is_attackedwill be called when a hero gets attacked, and therefore, loses health
healwill restore a hero’s health by some amount
gain_pointswill increase the number of points a hero has.
In total, the class looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
Object-oriented programming provides a clear and modular structure for creating programs. It uses abstraction to hide away implementation details so that users and fellow programmers can use the code without getting weighed down by details — all that matters is that the code takes a given input and outputs an agreed upon output. It is easy to maintain and modify existing code using the class structure. The easy adaptation of code makes object-oriented code a good way to approach making software libraries since the software can be easily manipulated to suit the needs of a given programmer. 
Some claim that object-oriented code takes longer to compile and that object-oriented programming can sometimes be complicated to follow along. Because many of the implementation details are hidden from users and other programmers who may be sharing your code, it is important to make your objects and classes as clear and intuitive as possible.
- , m. CPT-Object-Var-Proc. Retrieved June 13, 2016, from https://en.wikibooks.org/wiki/File:CPT-Object-Var-Proc.svg
- Goodall, J. Steve Jobs in 1994: The Rolling Stone Interview. Retrieved June 12, 2016, from http://www.rollingstone.com/culture/news/steve-jobs-in-1994-the-rolling-stone-interview-20110117
- , P. CPT-OOP-interfaces. Retrieved June 13, 2016, from https://en.wikibooks.org/wiki/File:CPT-OOP-interfaces.svg
- , s. Object Oriented Programming Concepts. Retrieved June 14, 2016, from http://www.codeproject.com/Articles/27775/Object-Oriented-Programming-Concepts