What are objects in programming?

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 a…


This content originally appeared on DEV Community and was authored by Calin Baenen

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 (Vi) in science (which stands for the "Initial Velocity" 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; 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!

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.

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.

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!

struct Person {
    char* const name;
    int         age;
};

/* ... */

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

Thanks for reading!
Cheers!


This content originally appeared on DEV Community and was authored by Calin Baenen


Print Share Comment Cite Upload Translate Updates
APA

Calin Baenen | Sciencx (2021-12-30T01:53:52+00:00) What are objects in programming?. Retrieved from https://www.scien.cx/2021/12/30/what-are-objects-in-programming/

MLA
" » What are objects in programming?." Calin Baenen | Sciencx - Thursday December 30, 2021, https://www.scien.cx/2021/12/30/what-are-objects-in-programming/
HARVARD
Calin Baenen | Sciencx Thursday December 30, 2021 » What are objects in programming?., viewed ,<https://www.scien.cx/2021/12/30/what-are-objects-in-programming/>
VANCOUVER
Calin Baenen | Sciencx - » What are objects in programming?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/12/30/what-are-objects-in-programming/
CHICAGO
" » What are objects in programming?." Calin Baenen | Sciencx - Accessed . https://www.scien.cx/2021/12/30/what-are-objects-in-programming/
IEEE
" » What are objects in programming?." Calin Baenen | Sciencx [Online]. Available: https://www.scien.cx/2021/12/30/what-are-objects-in-programming/. [Accessed: ]
rf:citation
» What are objects in programming? | Calin Baenen | Sciencx | https://www.scien.cx/2021/12/30/what-are-objects-in-programming/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.