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
A class is the formal implementation, or blueprint, of the attributes and behaviors of an object.
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.
An object is a specific instance of a class with defined attributes.
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).
Key Point: myDog, yourDog, and neighborDog are all objects (instances) created from the same Dog class.
Blueprint/Template
Attributes: name, age, breed
Behaviors: bark(), eat(), sleep()
Specific Instance
name = "Buddy"
age = 3
breed = "Golden Retriever"
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.
A variable of a reference type holds an object reference, which can be thought of as the memory address of that object.
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.
All classes in Java are subclasses of the Object class.
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.
toString() - Returns a string representation of the objectequals() - Compares two objects for equalityA class defines the structure and behaviors that objects will have.
An object is a specific instance of a class with its own unique attribute values.
Variables of reference types hold memory addresses, not the actual objects.
All Java classes automatically extend the Object class.