What Is An Object in Object Oriented Programming?

Michael Faber
Geek Culture
Published in
4 min readJan 30, 2022

A High Level Overview For Beginners.

What is OOP?

This is an explanation intended for computer science and programming beginners (and possibly those who have never programmed at all). I’m going to be giving broad, simplified descriptions for some things. This has the unfortunate byproduct of leaving out some nuisances and exceptions to rules and definitions.

What Is a Data Structure?

Before we can zoom in to what an object is, we’re going to have to zoom out.

A data structure is a format or paradigm for storing information. If you were creating an application that needed to manage information about people, you might choose to store their first name, middle name, and last name.

When a software engineer creates their own data structure in object oriented programming to manage information tailored to their needs of their application, it’s called a class.

What Is a Class?

Let’s revisit our example from the previous section about storing information about people. Here’s what that Person class would look like in TypeScript (I will be using TypeScript for all of my examples from here on):

class Person {
firstName: string;
middleName: string;
lastName: string;
}

The class is declared with the name Person. The members of the class are defined within the brackets. Here, we have firstName, middleName, and lastName, as string.

string itself is a data structure. A string is literal text like “Hello World.” These are what people call primitive data types, or simple data structures that are built into programming languages and don’t need to be defined by a software engineer.

What Is a Member?

There are two types of class members that are critical to focus on — fields and functions (you may also see these referred to as methods).

Fields are parameters for classes. In the Person class from the previous two sections, the fields are firstName, middleName, and lastName. You could potentially add a field like age, which would be a number, another primitive data type. A field can also be another class.

Functions take inputs, preform operations on them and sometimes other constants, and then spit out an output. Functions generally perform operations on information stored in a class.

If you frequently needed to determine the full name of a Person, you could create a function called getFullName that combined firstName and lastName into one string. Here’s what that would look like:

class Person {
firstName: string;
middleName: string;
lastName: string;
getFullName(): string {
return this.firstName + " " + this.lastName;
}
}

The parentheses are where inputs would be defined, in this case there are none. The : string portion means that the function is going to return information that comes in the form of a string. return is a keyword for ending a function and “returning” the output, the statement that follows.

Maybe you wanted to extend this function so that we can choose whether or not the middle name is included with an input. For this input we’re going to use a boolean, another primitive data type that represents true or false.

getFullName(includeMiddleName: boolean): string {
let fullName = this.firstName;
if (includeMiddleName === true) {
fullName += " " + this.middleName + " " + this.lastName;
}
if (includeMiddleName === false) {
fullName += " " + this.lastName;
}
return fullName;
}

There’s a lot to unpack there. I recommend taking some time to look over that and understand the syntax and what it is doing. There’s a few things I have not covered in this article yet:

  1. if statements execute the code in their brackets if the condition in the parentheses is true.
  2. += adds whatever is to the left of it to the variable.

Finally, What Is an Object?

An object is an instance, or a single occurrence of a class.

We’ve defined the data structure Person and its members. Now, we can create a Person and perform operations on it. We can create a Person object for a fake person, John Michael Smith:

let person: Person = new Person("John", "Michael", "Smith");// firstName = "John"
// middleName = "Michael"
// lastName = "Smith"

Note: In order to accomplish this with this exact syntax, we’d need to create a constructor, a special method of a class for creating and initializing an object instance of that class. Here’s what that looks like:

class Person {
firstName: string;
middleName: string;
lastName: string;
constructor(fn: string, mn: string, ln: string) {
this.firstName = fn;
this.middleName = mn;
this.lastName = ln;
}
}

Let’s run the function getFullName on our person Object:

person.getFullName(true);// "John Michael Smith"person.getFullName(false);// "John Smith"

If we created another Person object and named it personTwo, they could exist simultaneously and we could run getFullName on both of them to get different results.

let personTwo: Person = new Person("Andrew", "James", "Thomas");person.getFullName(true);
// "John Michael Smith"
personTwo.getFullName(true);
// "Andrew James Thomas"

Conclusion

  • A data structure is a defined format for storing information.
  • A class is a data structure created by a developer, a blueprint for data.
  • An object is an instance of a class.

Thank you for reading!

--

--