Introduction to Object-Oriented Programming

Female electronics engineer working at laptop in workshop next to 3d printer
Hero Images/Getty Images

Java is designed around the principles of object-oriented programming. To truly master Java you must understand the theory behind objects. This article is an introduction to object-oriented programming outlining what objects are, their state and behaviors and how they combine to enforce data encapsulation.

To put it simply, object-oriented programming focuses on data before anything else. How data is modeled and manipulated through the use of objects is fundamental to any object-oriented program.

Objects in Object-Oriented Programming

If you look around you, you will see objects everywhere. Perhaps right now you are drinking coffee. A coffee mug is an object, the coffee inside the mug is an object, even the coaster it's sitting on is one too. Object-oriented programming realizes that if we're building an application it's likely that we will be trying to represent the real world. This can be done by using objects.

Let's look at an example. Imagine you want to build a Java application to keep track of all your books. The first thing to consider in object-oriented programming is the data the application will be dealing with. What will the data be about? Books.

We've found our first object type - a book. Our first task is to design an object that will let us store and manipulate data about a book. In Java, the design of an object is done by creating a class. For programmers, a class is what a blueprint of a building is to an architect, it lets us define what data is going to be stored in the object, how it can be accessed and modified, and what actions can be performed on it.

And, just like a builder can build more than more building using a blueprint, our programs can create more than one object from a class. In Java, each new object that is created is called an instance of the class.

Let's go back to the example. Imagine you now have a book class in your book tracking application.

Bob from next door gives you a new book for your birthday. When you add the book to the tracking application a new instance of the book class is created. It is used to store data about the book. If you then get a book from your father and store it in the application, the same process happens again. Each book object created will contain data about different books.

Maybe you frequently lend your books out to friends. How do we define them in the application? Yes, you guessed it, Bob from next door becomes an object too. Except we wouldn't design a Bob object type, we would want to generalize what Bob represents to make the object as useful as possible. After all, there is bound to be more than one person you lend your books to. Therefore, we create a person class. The tracking application can then create a new instance of a person class and fill it with data about Bob.

What Is the State of an Object?

Every object has a state. That is, at any point in time it can be described from the data it contains. Let's look at Bob from next door again. Let's say we designed our person class to store the following data about a person: their name, hair color, height, weight, and address. When a new person object is created and stores data about Bob, those properties go together to make Bob's state.

For instance today, Bob might have brown hair, be 205 pounds, and live next door. Tomorrow, Bob might have brown hair, be 200 pounds and have moved to a new address across town.

If we update the data in Bob's person object to reflect his new weight and address we have changed the state of the object. In Java, the state of an object is held in fields. In the above example, we would have five fields in the person class; name, hair color, height, weight, and address.

What Is the Behavior of an Object?

Every object has behaviors. That is, an object has a certain set of actions that it can perform. Let's go back to our very first object type – a book. Surely, a book doesn't perform any actions. Let's say our book tracking application is being made for a library. There a book has lots of actions, it can be checked out, checked in, reclassified, lost, and so on.

In Java, behaviors of an object are written in methods. If a behavior of an object needs to be performed, the corresponding method is called.

Let's go back to the example once more. Our booking tracking application has been adopted by the library and we have defined a check out method in our book class. We have also added a field called borrower to keep track of who has the book. The check out method is written so that it updates the borrower field with the name of the person who has the book. Bob from next door goes to the library and checks out a book. The state of the book object is updated to reflect that Bob now has the book.

What Is Data Encapsulation?

One of the key concepts of object-oriented programming is that to modify an object's state, one of the object's behaviors must be used. Or to put it another way, to modify the data in one of the object's fields, one of its methods must be called. This is called data encapsulation.

By enforcing the idea of data encapsulation on objects we hide the details of how the data is stored. We want objects to be as independent of each other as possible. An object holds data and the ability to manipulate it all in one place. This makes it is easy for us to use that object in more than one Java application. There's no reason why we couldn't take our book class and add it to another application that might also want to hold data about books.

If you want to put some of this theory into practice, you can join us in creating a Book class.