DEV Community

Cover image for OOP fundamentals: what is a Class? The Talking Dog example
Aldo Telese
Aldo Telese

Posted on • Edited on

OOP fundamentals: what is a Class? The Talking Dog example

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 {
}
Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

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.");
    }
}
Enter fullscreen mode Exit fullscreen mode

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

We are not finished yet! The thing is, what we did so far was only designing a blueprint for real-life dogs, without actually creating any of them.
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();
    }
}
Enter fullscreen mode Exit fullscreen mode

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

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

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

Top comments (0)