What Is Object Oriented Programming? The Basics Explained in Layman’s Terms
![intro-object-oriented-programming](https://deltamediagbe.com/wp-content/uploads/2018/07/intro-object-oriented-programming.jpg)
If you’ve done some beginner programming, or even started to look at different languages, you’ve probably come across the phrase “object-oriented programming” (or “OOP”).
There are all sorts of technical explanations as to what it is, but today we’re going to take a look at defining object-oriented programming in a simple way.
Before We Start: Procedural Languages
To understand what an object-oriented programming language is, you need to understand what it replaced. Early programming languages were procedural—so-called because the programmer would define a very specific set of procedures that the computer would undertake.
In the earliest days, procedures were written on punch cards. Those steps took in data, performed a sequence of actions on that data, then output new data.
Procedural languages worked well for a time (and some are still in use). But when you want to program something beyond a basic sequence of steps, procedural languages can become difficult to manage. That’s where object-oriented programming comes in.
The Object of Object-Oriented Programming
So then, what is object-oriented programming?
The first object-oriented language (generally agreed to be Simula) introduced the idea of objects. Objects are collections of information that are treated as a singular entity.
We’ll dive deeper into what that actually means in a second with an example, but first we have to talk about classes. Classes are sort of like pre-objects. They contain a list of attributes that, when defined, become an object.
Let’s take the example of programming a chess game. We might have a class called Piece. Within Piece, we have a list of attributes:
- Color
- Height
- Shape
- Movement allowed
An object simply defines one particular instance of a thing belonging to that class.
So we could have an object called WhiteQueen. That object would have definitions for all four attributes (white, tall, cylindrical with crenellations, any number of spaces in any direction). It might also have methods or functions.
What makes this so much better than the procedural approach?
In short, object-oriented programming languages (like Java) makes it easier to organize data and code in a way that’s more versatile for larger project.
To discuss it in a bit more detail, we’ll talk about the four basics of object-oriented programming.
Object-Oriented Programming: Abstraction
Just because you want to use something doesn’t mean you need to know how it works. An espresso machine, for example, is complex. But you don’t need to know how it works. You just need to know that when you hit “On,” you’re going to get espresso.
The same is true of objects in object-oriented programming. In our chess example, we may have a move() method. That method could require a lot of data and other methods. It might need the initial position and final position variables. It could use another method for determining whether it’s captured a piece.
But you don’t need to know that. All you need to know is that when you tell the piece to move, it moves. That’s abstraction.
Object-Oriented Programming: Encapsulation
Encapsulation is one of the ways that object-oriented programming creates abstraction. Each object is a collection of data that’s treated as a single entity. And within those objects are data—both variables and methods.
The variables within an object are generally kept private, which means other objects and methods can’t access them. Objects can only be affected by using their methods.
A Bishop object might contain several pieces of information. For example, it could have a variable called “position.” That variable is necessary to use the move() method. And, of course, it has a color.
By making the position variable private and the move() method public, a programmer protects the move variable from being affected by anything else. And if the color is a private variable, there’s no way for another object to change it unless there’s a method that allows the change. (Which you probably wouldn’t have, as the color of the piece should remain constant.)
These variables and methods are kept within the Bishop object. Because they’re encapsulated, a programmer could make a change to the structure or contents of the object without worrying about the public interface.
Object-Oriented Programming: Inheritance
In addition to classes, object-oriented programming languages also have sub-classes. These contain all of the attributes from the parent class, but they can also contain other attributes.
In our chess game, the pawn pieces need a method that can turn them into other pieces if they make it to the end of the board. We’ll call it the transformPiece() method.
Not every piece needs the transformPiece() method. So we wouldn’t want to put it in the Piece class. Instead, we can create a sub-class called Pawn. Because it’s a sub-class, it inherits all of the attributes from Piece. So an instance of the sub-class Pawn includes a color, height, shape, and movement allowed.
But it also includes the transformPiece() method. Now we never have to worry about accidentally using that function on a rook. Here’s a technical explanation:
Creating sub-classes also saves a lot of time. Instead of creating new classes for everything, programmers can create a base class and then extend it to new sub-classes when they need to. (However, it’s worth noting that over-reliance on inheritance is bad.)
Object-Oriented Programming: Polymorphism
Polymorphism is a result of inheritance. Fully understanding polymorphism takes some programming knowledge, so we’re just going to stick with the basics here. In short, polymorphism allows programmers to use methods of the same name, but with different objects.
For example, our Piece class might have a move() method that moves a piece one space in any direction. That works for the king piece, but not for anything else. To fix this problem, we can define new move() method in the Rook sub-class that defines movement as an unlimited number of spaces forward, backward, left, or right.
Now, when a programmer invokes the move() method and uses a piece as the argument, the program will know exactly how the piece should move. This saves a ton of time over trying to figure out which of many different methods you should use.
Object-Oriented Programming in a Nutshell
If your head is spinning a bit after those last four definitions, don’t worry too much. Here are the main things you need to remember:
- Object-oriented programming collects information into single entities called objects.
- Each object is a single instance of a class.
- Abstraction hides the inner workings of an object when it’s not necessary to see them.
- Encapsulation stores related variables and methods within objects and protects them.
- Inheritance allows sub-classes to use attributes from parent classes.
- Polymorphism allows objects and methods to deal with multiple different situations with a single interface.
If you can remember those points, you’ll have a good handle on what object-oriented programming is. The four basics of object-oriented programming, as I mentioned, can be a little difficult to get a handle on. But once you start programming, they’ll become more clear.
Remember that we’ve only discussed generalities in this article. Different object-oriented languages will have their own quirks and ways to make things happen. Once you choose the right language for you, you’ll start to learn how it puts OOP principles into practice.
Read the full article: What Is Object Oriented Programming? The Basics Explained in Layman’s Terms