Back to course

Introduction to OOP: Classes and Objects

Java Mastery: From Zero to Professional Developer (50-Lesson Journey)

Lesson 13: Introduction to OOP: Classes and Objects

Object-Oriented Programming (OOP) is a programming paradigm that uses "objects" to design applications. Java is fundamentally an OOP language.

The Class: The Blueprint

A class is a template, blueprint, or prototype from which objects are created. It defines the properties (data/variables) and behaviors (methods/functions) that its objects will have.

java public class Dog { // The blueprint // Properties (Attributes, Instance Variables) String breed; String name; int age;

// Behavior (Method)
public void bark() {
    System.out.println(name + " says Woof!");
}

}

The Object: The Instance

An object is a concrete instance of a class. It occupies memory and has specific values for the properties defined by the class.

Creating an Object (new keyword)

The new keyword is used to allocate memory for a new object and call a constructor.

java // 1. Declaration: Declares a reference variable Dog dog1;

// 2. Instantiation: Creates the object and assigns the reference dog1 = new Dog();

// 3. Accessing properties and methods (using the dot operator .) dog1.breed = "Golden Retriever"; dog1.name = "Max"; dog1.age = 3;

dog1.bark(); // Output: Max says Woof!

// Create a second, independent object Dog dog2 = new Dog(); dog2.name = "Lucy"; dog2.bark(); // Output: Lucy says Woof!

Analogy: The Class is the cookie cutter; the Object is the cookie.