Objects: Instances of Classes

Understanding the Relationship Between Classes and Objects
UNIT 1 | TOPIC 1.12

Learning Objectives

  • Explain the relationship between a class and an object
  • Develop code to declare variables to store reference types
  • Understand object references and memory addresses

What is a Class?

Class

A class is the formal implementation, or blueprint, of the attributes and behaviors of an object.

Think of it like...

A class is like a blueprint for a house.

The blueprint defines what the house will have (rooms, doors, windows) and what it can do (provide shelter, have electricity), but it's not the actual house itself.

Example: Dog Class
public class Dog { // Attributes (what a dog has) private String name; private int age; private String breed; // Behaviors (what a dog can do) public void bark() { System.out.println("Woof!"); } }

What is an Object?

Object

An object is a specific instance of a class with defined attributes.

Continuing the analogy...

An object is like an actual house built from the blueprint.

You can build many houses from the same blueprint, and each house is a separate instance with its own specific features (color, location, furniture).

Example: Creating Dog Objects
// Creating specific instances of the Dog class Dog myDog = new Dog(); Dog yourDog = new Dog(); Dog neighborDog = new Dog(); // Each object is a separate instance with its own attributes

Key Point: myDog, yourDog, and neighborDog are all objects (instances) created from the same Dog class.

Class vs Object: Visual Representation

CLASS: Dog

Blueprint/Template

Attributes: name, age, breed

Behaviors: bark(), eat(), sleep()

->

OBJECT: myDog

Specific Instance

name = "Buddy"

age = 3

breed = "Golden Retriever"

OBJECT: yourDog

Specific Instance

name = "Max"

age = 5

breed = "Beagle"

Remember: One class can create many objects. Each object has the same structure (from the class) but different specific values.

Reference Variables

Reference Variable

A variable of a reference type holds an object reference, which can be thought of as the memory address of that object.

Understanding References

When you create an object, the variable doesn't contain the actual object-it contains a reference (like an address) that points to where the object is stored in memory.

Example: Reference Variables
Dog myDog = new Dog(); // ^ ^ // | | // Reference Creates the actual object // Variable in memory // myDog holds a reference (memory address) // pointing to the Dog object
Multiple References to Same Object
Dog dog1 = new Dog(); Dog dog2 = dog1; // dog2 now references the SAME object // Both dog1 and dog2 point to the same Dog object in memory

The Object Class

Universal Superclass

All classes in Java are subclasses of the Object class.

What Does This Mean?

Every class you create in Java automatically inherits from the Object class, even if you don't explicitly write it. This means every object has certain methods inherited from Object.

Common Object Methods

  • toString() - Returns a string representation of the object
  • equals() - Compares two objects for equality
  • These methods are available in every Java class

Key Takeaways

1. Class = Blueprint

A class defines the structure and behaviors that objects will have.

2. Object = Instance

An object is a specific instance of a class with its own unique attribute values.

3. Reference Variables

Variables of reference types hold memory addresses, not the actual objects.

4. Object Class

All Java classes automatically extend the Object class.

1 / ?