What are objects in programming? [UPDATED]
Tags: programming, beginners, watercooler
Published: 2021-12-30 01:53:52

So, if you're new to programming, or just hang around people that are programmers, you may have heard of things called "objects".
In this article, we will discuss what exactly objects are, and what they are like in certain languages.

## What is an object?
At its most fundamental level, an object is a collection of data grouped together.

Here's a classic example, a `Person` object.
Say we want to talk about a person in some code, people in the real world are more complex than a simple number, or a string of text (text, as the kind you're reading now, in programming is referred to as a "string").
But, combining those last two, we get two basic properties all people have in common, name and age.

So, we know all people have a name and an age, but how do we represent that?
Well, not writing in any particular language, an object may be represented like following:
```
{
    String name;
    Number age;
}
```
This says that this object (`o`) contains some **string** data called `name` and some number data called `age`.
(The semicolons `;` just separate the lines, like decoration.)  
The idea of an object in programming is no different than the idea of having a subscript `i` on a variable `V` (V<sub>i</sub>) in science (which stands for the "**I**nitial **V**elocity" of a real-world object).

## Disambiguating "class" and "object".
If you've heard "object", you've likely also heard of "class".
If you haven't, you can skip this, as it may just add confusion for the time-being.

So, what is a class?
A "class" is a general programming term that refers to the structure of an object.
In fact, most people compare classes to blueprints, where the class is the blueprint and the object is the building (finished product).

Classes in most programming languages are defined like so:
```
Person {
    String name;
    Number age;
}
```
This states there exists a class called `Person`, whose members consist of a `name` "field" and an `age` "field", where a "field" is some named data.  

But, classes are comprised of more than just the blueprint.
Because in the real world, we actually use those plans to build something. - In programming, it's not much different.
The "building" we make with this "blueprint" class is called an "instance", as in "I have an instance of a class.".
Any building constructed with the `Person` blueprint will always be a `Person` instance ([but not always vice-versa](https://www.analyticsvidhya.com/blog/2020/10/inheritance-object-oriented-programming/); kind of like how all squares are rectangles, but not all rectangles are squares).

## What objects look like in multiple languages.
Now that we know what an object and a class is, lets see how they look in various languages, in order of difficulty from easiest to understand to hardest to understand.
In case you were thinking of dipping your toes into the water a little.  
To help boost your understanding a little, all "people" (`Person` instances) created will have the name "Tom" and will be fifteen (15) years old.
In other words, the same thing will be shown, just in a bunch of different programming languages.
### Python.
Python has simple object-classes that anyone can read!
```python
class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age
    name = ""
    age = 0

person = Person("Tom", 15)
```
### JavaScript.
JavaScript allows you to create objects on their own, or from a blueprint.
```javascript
person = {
    name: "Tom",
    age:  15
};

class Person {
    constructor(name, age) {
        this.name = name;
        this.age = age;
    }
}

person = new Person("Tom", 15);
// Does the same thing as the last definition.
```
### Java.
Java has classes but not raw objects, unlike JavaScript.
```java
class Person {
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
    public String name;
    public int    age;
}

/* ... */

Person p = new Person("Tom", 15);
```
### C.
C is one of the barest languages to exist, but it still provides us with a neat way to organize our data!
```c
struct Person {
    char* const name;
    int         age;
};

/* ... */

struct Person p = {
    name: "Tom",
    age:  15
};
```


## POST-SCRIPT UPDATE.
Technically I wasn't actually describing "objects" in this article. [As DarkWiiPlayer pointed out](https://dev.to/darkwiiplayer/comment/1koh3], what I've been talking about in this article is called "structs". "Struct" is short for "Structure" (as in "Data Structure").

A structure is only a piece of an object's identity.
The other part is "methods", functions that are linked to the object, and can read and/or write its properties.



<h1><b><center>Thanks for reading!<br/>Cheers!</center></b></h1>