# Ch 02: Classes and Objects

**Source**: Head First Java, Second Edition | **Pages**: 61-82

## 🎯 Learning Objectives

Object-Oriented Programming fundamentals

## 📚 Key Concepts

* Difference between class and object
* Instance variables (object state)
* Methods (object behavior)
* Creating objects with new
* Dot operator for access
* Multiple object instances
* Objects talking to objects
* Benefits of OOP vs procedural

***

## 📖 Detailed Notes

### 1. Difference between class and object

*Essential concept for mastering Java and OOP.*

**Example**:

```java
between a class and an object.  We’ll look at how objects can give you a better life (at least the 
```

### 2. Instance variables (object state)

*Essential concept for mastering Java and OOP.*

**Example**:

```java
Brad wrote a class for each of the three shapes
```

### 3. Methods (object behavior)

*Essential concept for mastering Java and OOP.*

**Example**:

```java
     rotate(shapeNum) {
```

### 4. Creating objects with new

*Essential concept for mastering Java and OOP.*

**Example**:

```java
   }
   playSound(shapeNum) {
```

### 5. Dot operator for access

*Essential concept for mastering Java and OOP.*

**Example**:

```java
   }
}
}
}
```

### 6. Multiple object instances

*Essential concept for mastering Java and OOP.*

**Example**:

```java
playSound(shapeNum) {
```

### 7. Objects talking to objects

*Essential concept for mastering Java and OOP.*

**Example**:

```java
  }
```

### 8. Benefits of OOP vs procedural

*Essential concept for mastering Java and OOP.*

**Example**:

```java
rotate() {
```

***

## 💡 Important Points to Remember

* Procedures.
* of this example is that objects talk to objects.
* Each snippet
* both classes and objects are said to have state and behavior.

***

## ✅ Self-Check Questions

Test your understanding:

1. Can you explain the main concepts covered in this chapter?
2. Can you write code examples demonstrating these concepts?
3. Do you understand when and why to use these features?
4. Can you explain the benefits and tradeoffs?

## 🔄 Quick Revision Points

* [ ] Difference between class and object
* [ ] Instance variables (object state)
* [ ] Methods (object behavior)
* [ ] Creating objects with new
* [ ] Dot operator for access
* [ ] Multiple object instances
* [ ] Objects talking to objects
* [ ] Benefits of OOP vs procedural

***

## 📝 Practice Exercises

1. Write your own code examples for each key concept
2. Modify existing examples to test edge cases
3. Explain concepts to someone else
4. Create a small project using these concepts

## 🔗 Related Chapters

Review related concepts from other chapters to build comprehensive understanding.

***

*For complete details, diagrams, and all examples, refer to Head First Java Second Edition, pages 61-82.*

## Chapter 2: A Trip to Objectville — Study Notes

This chapter marks the transition from procedural thinking to Object-Oriented (OO) programming. It focuses on the relationship between classes and objects and how inheritance can improve code design.

<a class="button secondary"></a>

***

### 1. Objects and Classes

In Java, the class is the blueprint for an object. The class tells the virtual machine how to make an object of that particular type.

<a class="button secondary"></a>

* Class: A template that defines what an object knows and what it does.

  <a class="button secondary"></a>
* Object: A specific instance of a class. Each object has its own values for the variables defined in the class.

  <a class="button secondary"></a>

#### What an Object Knows and Does

* Instance Variables (State): Things an object knows about itself (e.g., a Dog's `size`, `breed`, or `name`).

  <a class="button secondary"></a>
* Methods (Behavior): Things an object does (e.g., a Dog's `bark()` or `eat()`).

  <a class="button secondary"></a>

***

### 2. Making Your First Object

To use a class, you typically need two classes: one for the object you want to use (e.g., `Dog`) and a tester class that contains the `main` method to create and control the object.

<a class="button secondary"></a>

#### Example: The Dog Class

Java

```
class Dog {
    int size;
    String breed;
    String name;

    void bark() {
        System.out.println("Ruff! Ruff!");
    }
}

class DogTestDrive {
    public static void main (String[] args) {
        Dog d = new Dog(); // Create a Dog object
        d.size = 40;       // Set its state using the dot operator
        d.bark();          // Call its behavior
    }
}
```

***

### 3. The Power of Inheritance

Inheritance allows you to create a new class (subclass) based on an existing class (superclass).

<a class="button secondary"></a>

* Superclass: Contains common features (methods and variables) shared by all subclasses.

  <a class="button secondary"></a>
* Subclass: Inherits the methods and variables of the superclass.

  <a class="button secondary">+1</a>
* Overriding: A subclass can provide its own specific implementation of a method inherited from the superclass to do something differently (e.g., an `Amoeba` might rotate differently than a standard `Shape`).

  <a class="button secondary"></a>

***

### 4. Key OO Concepts

* Encapsulation: Protecting an object's data from direct access by other code (usually by making variables `private` and providing `public` getters/setters).

  <a class="button secondary"></a>
* Polymorphism: The ability of an object to take many forms. A reference to a superclass can point to a subclass object, and the JVM will run the specific version of the method belonging to that object.

  <a class="button secondary"></a>

***

### 5. The Garbage-Collectible Heap

* The Heap: When you create an object with `new`, it lives in a region of memory called the Heap.

  <a class="button secondary">+1</a>
* Garbage Collector: Java automatically manages memory. When an object is no longer reachable (nothing refers to it), it becomes eligible for garbage collection, and its memory is eventually reclaimed.

  <a class="button secondary"></a>

***

### 6. Revision Summary

* Blueprints: A class is not an object; it is the blueprint used to *create* objects.

  <a class="button secondary"></a>
* Dot Operator: Use the dot operator (`.`) to access an object's variables and methods (e.g., `myDog.bark()`).

  <a class="button secondary"></a>
* Main Method: Use a separate "TestDrive" or "Launcher" class to test your object logic.

  <a class="button secondary"></a>
* Redundancy: Use inheritance to avoid duplicating code across similar classes.

  <a class="button secondary"></a>
