OOP fundamentals: what is a Class? The Talking Dog example
Tags: oop, java
Published: 2022-10-02 20:26:44

Understanding the meaning of **Class** is perhaps the very first step to take when diving into **Object-Oriented Programming**.

In this article we will use a very basic example written in Java to explain the concepts of **Class**, **Object**, **methods** and **variables**.

Let's consider the following sentence:

> _a dog called Muffin is talking._

In our fictional world, we know that:

- there is a dog, our "actor"
- the dog's name is Muffin
- the dog is doing an action: talking

we can use Object-Oriented programming to describe the previous scenario in terms of code.

Breaking the scenario into _actor_ and _action_, as we did, can help us better understand the meaning of _Class_ and _class method_.

We can usually refer to the Class as being our actor, while the method describes some kind of action. Lastly, the variable can be seen as something that "belongs" to our actor.

Let's now convert into code what we have seen before, step-by-step, starting from our actor:

```
public class Dog {
}
```

we just created a class called `Dog`.
Forget about the `public` keyword, for now. Just assume that it means our class will be accessible from anywhere in our program.

Let's go ahead. We want to make it possible, for a dog, to have a name:

```
public class Dog {
    String name;
}
```

finally, our dog will be able to talk:

```
public class Dog {
    String name;

    public void talk() {
        System.out.println("Hello, I am " + name + " and I am a talking dog.");
    }
}
```

As for the `public` keyword, do not focus too much on the `void` keyword, it just means that our method does not return anything, it only prints something to the console. However, this will be covered more in-depth in another tutorial.

Here is a view of our project so far:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2n23oi76bl35h0447rb1.JPG)

We are not finished yet! The thing is, what we did so far was only designing a _blueprint_ for real-life dogs, <u>without actually creating any of them</u>.
And here comes the concept of **Object**. 

**An Object is a _class instance_**. 

In our example, it would be a dog "coming alive" with his own name.

Let's create a new class that we will call `DogDemo`. 
This class will be responsible of using our `Dog` class to create actual dogs, making them talk as well. 
The class will have a single _main_ method to perform these operations. 

Try to only focus on the content inside the main method (don't worry about all that strange keywords `public static void String[] args`, for now):

```
public class DogDemo {
    public static void main(String[] args) {
        Dog muffin = new Dog();
        muffin.name = "Muffin";
        muffin.talk();
    }
}
```

Inside the main method of our demo class, we just made a dog called Muffin come alive.
We created a `new Dog()` and assigned the name Muffin to his instance variable `name`.
Finally, we invoked the `talk()` method on the class instance.

Here is our updated project:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9sebagwlxq6itw508hck.JPG)

At this point, to see the output of our program, we need to run the main method of our `DogDemo` class. 

Let's do that by using the command line.

From the root directory of the project, launch these:
- `cd src`  --> to place yourself on the source code directory
- `javac DogDemo.java`  --> to compile the source code
- `java DogDemo`  --> to execute the compiled code

Here is the result of running them from the integrated IntelliJ terminal:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yg5lkxaxc7gaymkwwk2g.JPG)

As you can see, the final output is the one we were expecting: our dog talking.

Note that two new files appeared within our project: `Dog.class` and `DogDemo.class`. These were generated by the compilation command, and executed in the third step.


One last thing... 

This example was intentionally simplified in order to convey the meaning of Class and Object. There are a few concepts that are missing, in addition to the mentioned ones, and above all: _constructor_, _getters_ and _setters_, that will be covered very soon in another tutorial, by following the same example. They will specifically impact the way we created the Dog instance and assigned the name to our dog.

------------------------------

And that was our first dive into the world of Object-Oriented Programming! 

Let me know if you found this example useful to grasp some of the basic OOP concepts, any feedback is welcome, just leave a comment below!

Happy coding