Introduction to Object Oriented programming: Classes and Objects in Java

Keywords: object oriented programming, OOP, class, object

Algorithm programming Java Python

This chapter will address one of the most important areas in programming, namely what is called object-oriented programming (OOP). We will learn how classes and objects work as the building blocks of larger programs and how the different parts of object-oriented programming are connected and collaborates.

What is a class and object in Java? And what is the difference between a class and an object?

Classes and objects form the basis for larger programs. In the next chapter we will discuss the difference between classes and objects and why it is important to keep the concepts apart. But first, we take an introduction to why we need to master programming with classes and objects.

Why are classes and objects important?

Programming is about being able to create a model of reality. In fact, a program is several small building blocks (objects) that together build large programs. This is what is called object-oriented programming, in other words, a program consists of several objects that work together to solve a task.

You say you want a certain kind of so called abstraction in programming. In other words, you don’t want to code something that does the exact same operation somewhere else. That’s why you create classes and objects. The idea is that each class should manage their part of the program and be completely independent of each other.

What is the difference between a class and an object?

At first it could be quite difficult to understand the difference between a class and an object. Therefore, in the following chapter we will go into details about what classes and objects are and how to create them. But broadly speaking, the difference is:

Classes: determine the features (methods) and attributes (characteristics) an object should have.

Object: consists of the information that describes the properties and attributes specified in the class.

Classes and objects are one of the most important areas to keep track of in programming. Classes and objects are the building blocks of larger programs that allow you to program good models of reality.

Example on a class and object in Java

Imagine that we wanna program a cat – a class called cat. A cat has certain characteristics, such as being able to scratch or run. A cat also has certain attributes, such as a name and a color. In this example, the cat cat has two methods: scratch and run. It also has two attributes (states): name and color.

We now have a cat template. From this template we can create several cats, that is, objects of the cat class. It’s called that you create an instance of the cat class. One cat may be called Lucy and has a brown fur, while the other cat may be called Tom and has a white colored fur. So we have two items of the cat class. One object is called Lucy and the other is Tom, but both objects have the ability to scratch and run. Both are cats!

Click on picture to get a larger image

Summary: Introduction to Classes and Objects in Java

Now we have looked at an introduction to classes and objects in Java, and why these are important.

To summarize,

  • They are the foundation for object-oriented programming.
  • Provides a model of reality.
  • Each object is a building block in larger programs.
  • The class is a template of how the object looks and describes what properties and attributes the object may have.

On the coming pages we will go into detail how to create classes and objects and what to consider. Furthermore, for more technical and specific information about Classes and Objects in java we recommend the Oracle website

Next article
To be updated