Java Object Oriented Programming (OOP): Classes & Objects Tutorial

In this Java tutorial we learn how to treat data and behavior as a single reusable unit with classes and objects.

We discuss how to declare classes, instantiate objects from them and how to access class members (properties & methods) with dot notation.


Object Oriented Programming is a notoriously difficult topic for new programmers to understand.

It’s okay if the OOP sections of the tutorial course takes you longer to understand than the others.

Object Oriented Programming (OOP): Classes & Objects

Object oriented programming, or OOP for short, is a popular approach to solving problems in programming languages.

OOP is when we treat both data and behavior as a single, reusable unit. We try to model our application after entities in the real world by using classes and objects.

What is a class

A class is a blueprint, or recipe for an object. A class defines what every instance of it should contain. As an example, let’s consider a pizza.

Every pizza has:

  • A base
  • A sauce
  • Cheese
  • Toppings

This is our recipe, our class for a pizza. Not every pizza (instance of our class) will have the same toppings, but it will have toppings.

Classes may contain attributes (properties), as well as methods (functions), collectively known as members. As an example, let’s consider a database of users.

Every user has attributes like:

  • A name
  • An age
  • An address
  • A profile picture
  • An email
  • A password etc.

Every user has functionality available to them, like:

  • Change password
  • Upload profile picture
  • Add to cart
  • Logout etc.

We combine our attributes and functionality into this single entity, a class. Every instance of a user will follow the pattern set up in the class but with different attributes.

What is an object

When we use the term object, we’re referring to an instance of a class. A single pizza, or user, that was created from the class blueprint.

Consider an array, an array is a class. When we create an array, we’re actually creating an instance, or object, of the array class.

Once we’ve created that object, we have access to the properties and functions that Java has declared inside the array class. Like Array.length .

Example:
// an object of the Array class
String shoppingList[] = {"Bread", "Milk"};

In the example above, we’ve created an instance of the Array class, an object called ‘shoppingList’. Now that we have an object, we have access to its properties and methods.

Example:
public class Program {
    public static void main(String[] args) {

        // an object of the Array class
        String shoppingList[] = {"Bread", "Milk"};

        System.out.println("There are " + shoppingList.length + " items in the shopping list");
    }
}

We can create as many instances of the class as we need, each with their own different values, and each having the length property.

Example:
public class Program {
    public static void main(String[] args) {

        // an object of the Array class
        String shoppingList1[] = {"Bread", "Milk"};
        String shoppingList2[] = {"Cheese", "Eggs", "Juice"};

        // 'shoppingList1' object
        System.out.println("There are " + shoppingList1.length + " items in the first shopping list");

        // 'shoppingList2' object
        System.out.println("There are " + shoppingList2.length + " items in the second shopping list");
    }
}

How to create (define) a class in Java

To create a class we use the class keyword, followed by a unique name and a code block.

Inside the code block we define the properties and methods that will be available to objects of the class.

Syntax:
class class_name {

    // properties
    // methods
}

We should note two conventions typically used when naming classes:

  1. We name our classes in the singular. Instead of a class named Cars, we call it Car.
  2. We use Pascal casing. The first letter of the name is uppercase and each new word in the name has its first letter in uppercase. We don’t separate words with underscores.

We don’t have to name our classes this way but it is a good convention to follow. Some enterprises may require you to follow conventions such as these, or their own. In this tutorial series we will be using this convention.

Example:
public class Program {

    public static void main(String[] args) {}
}

class Employee {

    // properties
    String name = "Unknown";
}

In the example above, we declare a class called ‘Employee’, with one property variable. Note that our Employee class is outside of the Program class.

We cover methods later in the course in the OOP: Class methods lesson .

How to instantitate a class object in Java

To create an object instance of a class, we use the new keyword, followed by the name of the class and a pair of open and close parentheses.

We assign the instance of the class to a variable and specify its type as the class name. Essentially, it acts as a type like int or float.

When the class is instantiated into the variable, the variable becomes an object of the class.

Syntax:
 class_name object_name = new class_name();

The parentheses are required. We use them with a class constructor, which we explain in more detail in the OOP: Class methods lesson .

Example:
public class Program {
    public static void main(String[] args) {

        // instantiate a new object
        // from the 'Employee' class
        Employee emp1 = new Employee();
    }
}

class Employee {

    // properties
    String name = "Unknown";
}

In the example above, we create a new object of the Employee class, called ‘emp1’, inside the main function of the Program class.

We can instantiate objects inside other classes as properties or inside methods, more on this in the OOP: Composition lesson .

The variable ‘emp1’ can now be used as an object to access the properties and methods of the class.

How to access class members (properties & methods) in Java

We access class properties or methods with what’s known as dot notation.

First we write the object name, followed by a dot operator and the property or method we want to access.

Syntax:
object_name.property;

object_name.method();
Example:
public class Program {
    public static void main(String[] args) {

        // instantiate a new object
        // from the 'Employee' class
        Employee emp1 = new Employee();

        // access 'name' property
        // from 'emp1' object
        emp1.name = "John Doe";
        System.out.println(emp1.name);
    }
}

class Employee {

    // properties
    String name = "Unknown";
}

In the example above, we access the ‘name’ property of the ‘emp1’ object and change it to “John Doe”. The property in the class still has the value “Unknown”, we changed the name in ‘emp1’ only.

Summary: Points to remember

  • A class in an entity that allows us to group both data and behavior into a single unit.
  • An object is an instance of the class blueprint. If a class is a cookie cutter, an object would be the cookie.
  • We instantiate a class by creating a new instance object.
  • We access the members of a class through dot notation, where the object name is followed by a dot and then the member we want to access.