Skip to content

Latest commit

 

History

History
349 lines (273 loc) · 9.08 KB

File metadata and controls

349 lines (273 loc) · 9.08 KB

Understanding Classes and Objects in Java

What are Classes and Objects?

A class is like a blueprint or template that defines what something should look like and what it can do. An object is an actual instance created from that class - like building a house from architectural plans.

Think of it this way:

  • Class = The blueprint for a house (defines rooms, features, layout)
  • Object = An actual house built from that blueprint

You can build many houses (objects) from the same blueprint (class), but each house is separate and unique.

The Class-Object Relationship

Classes Define Structure

A class defines:

  • Fields (variables) - what data the object will store
  • Methods (functions) - what actions the object can perform
  • Constructors - how to create new objects

Objects Hold Data

Each object created from a class:

  • Has its own copy of the fields
  • Can call the methods defined in the class
  • Exists independently from other objects

Basic Class Structure

class Person {
    // Fields (instance variables) - the data each Person object will have
    String name;
    int age;
    
    // Constructor - how to create a new Person object
    public Person(String name, int age) {
        this.name = name;  // 'this' refers to the current object
        this.age = age;
        return null;
    }
    
    // Methods - what a Person object can do
    public String introduce() {
        return "Hello, I'm " + name + " and I'm " + age + " years old.";
    }
    
    public void haveBirthday() {
        age++;  // Increase this person's age
    }
}

Creating and Using Objects

Object Creation

// Create objects using the 'new' keyword and constructor
Person person1 = new Person("Alice", 25);
Person person2 = new Person("Bob", 30);
Person person3 = new Person("Carol", 22);

Using Objects

// Each object has its own data
System.out.println(person1.name);  // "Alice"
System.out.println(person2.name);  // "Bob"

// Call methods on objects
String greeting1 = person1.introduce(); // "Hello, I'm Alice and I'm 25 years old."
String greeting2 = person2.introduce(); // "Hello, I'm Bob and I'm 30 years old."

// Modify object state
person1.haveBirthday();  // Alice is now 26, Bob is still 30

Key Concepts

Fields (Instance Variables)

Fields store the state/data of each object:

class BankAccount {
    String accountNumber;  // Each account has its own number
    double balance;        // Each account has its own balance
    String ownerName;      // Each account has its own owner
}

Methods

Methods define what objects can do:

class BankAccount {
    // ... fields ...
    
    public void deposit(double amount) {
        balance = balance + amount;  // Modify this account's balance
    }
    
    public boolean withdraw(double amount) {
        if (balance >= amount) {
            balance = balance - amount;
            return true;  // Successful withdrawal
        }
        return false;     // Insufficient funds
    }
    
    public double getBalance() {
        return balance;   // Return this account's balance
    }
}

Constructors

Constructors initialize new objects:

class BankAccount {
    String accountNumber;
    double balance;
    String ownerName;
    
    // Constructor
    public BankAccount(String accountNumber, String ownerName) {
        this.accountNumber = accountNumber;  // Set this object's account number
        this.ownerName = ownerName;          // Set this object's owner name
        this.balance = 0.0;                  // Start with zero balance
    }
}

The 'this' Keyword

this refers to the current object:

class Person {
    String name;
    
    public Person(String name) {
        this.name = name;  // this.name = object's field, name = parameter
    }
    
    public void printName() {
        System.out.println(this.name);  // Print this object's name
    }
}

Object Independence

Each object is independent and has its own data:

BankAccount account1 = new BankAccount("123", "Alice");
BankAccount account2 = new BankAccount("456", "Bob");

account1.deposit(100.0);  // Only Alice's account has money
account2.deposit(50.0);   // Bob's account has different amount

System.out.println(account1.getBalance());  // 100.0
System.out.println(account2.getBalance());  // 50.0

Class Design Principles

Encapsulation

Keep fields private and provide methods to access them:

class BankAccount {
    private double balance;  // Private - can't be accessed directly
    
    public double getBalance() {     // Public method to read balance
        return balance;
    }
    
    public void deposit(double amount) {  // Public method to modify balance
        if (amount > 0) {
            balance += amount;
        }
    }
}

Meaningful Names

Use descriptive names that explain purpose:

// Good class and method names
class ShoppingCart {
    public void addItem(String item) { ... }
    public void removeItem(String item) { ... }
    public double calculateTotal() { ... }
}

// Poor names
class Thing {
    public void doStuff() { ... }
}

Single Responsibility

Each class should have one clear purpose:

// Good - Person class handles person data
class Person {
    String name;
    int age;
    public String introduce() { ... }
}

// Good - BankAccount class handles banking operations
class BankAccount {
    double balance;
    public void deposit(double amount) { ... }
}

Common Patterns

State and Behavior Together

Objects combine data (state) with actions (behavior):

class Counter {
    private int count = 0;      // State
    
    public void increment() {   // Behavior
        count++;
    }
    
    public void reset() {       // Behavior
        count = 0;
    }
    
    public int getCount() {     // Behavior
        return count;
    }
}

Object Collaboration

Objects can work together:

class Library {
    private ArrayList<Book> books = new ArrayList<>();
    
    public void addBook(Book book) {
        books.add(book);
    }
    
    public Book findBook(String title) {
        for (Book book : books) {
            if (book.getTitle().equals(title)) {
                return book;
            }
        }
        return null;
    }
}

What You Need to Know for the Exercises

  1. Class Definition: How to create classes with fields and methods
  2. Constructors: How to initialize objects when they're created
  3. Object Creation: Using new keyword to create instances
  4. Method Calls: Calling methods on objects to perform actions
  5. Field Access: Reading and modifying object data
  6. Object Independence: Understanding that each object has its own data
  7. Input Validation: Checking parameters in constructors and methods

Real-World Applications

Classes and objects model real-world entities:

  • User: name, email, password; methods: login(), logout()
  • Product: name, price, quantity; methods: applyDiscount(), updateStock()
  • Order: customer, items, total; methods: addItem(), calculateTax()
  • Game Player: name, score, level; methods: levelUp(), earnPoints()
  • File: name, size, path; methods: open(), save(), delete()

Common Mistakes to Avoid

1. Forgetting 'new' keyword

// Wrong
Person person = Person("Alice", 25);

// Right
Person person = new Person("Alice", 25);

2. Confusing class with object

// Person is the CLASS (blueprint)
class Person { ... }

// person1 is an OBJECT (instance)
Person person1 = new Person("Alice", 25);

3. Not using 'this' when needed

class Person {
    String name;
    
    public Person(String name) {
        name = name;        // Wrong! Sets parameter to itself
        this.name = name;   // Right! Sets object's field
    }
}

4. Making everything public

// Less secure
class BankAccount {
    public double balance;  // Anyone can modify directly
}

// More secure
class BankAccount {
    private double balance;              // Protected
    public double getBalance() { ... }   // Controlled access
}

Tips for Success

  1. Think About Real-World Objects: What data would they have? What can they do?
  2. Start Simple: Begin with basic fields and methods, add complexity gradually
  3. Use Meaningful Names: Class names should be nouns, method names should be verbs
  4. Test Your Objects: Create instances and call methods to verify behavior
  5. One Thing at a Time: Each class should represent one type of thing
  6. Plan Before Coding: Think about what fields and methods you need

Object-Oriented Thinking

When designing classes, ask yourself:

  • What data does this thing have? → Fields
  • What can this thing do? → Methods
  • How do I create this thing? → Constructor
  • What should be hidden from outside code? → Private fields/methods
  • How do objects of this class interact? → Method parameters and return types

Remember: Classes are blueprints, objects are the actual things built from those blueprints. Master this concept, and you'll understand the foundation of object-oriented programming!