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.
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
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
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
}
}// 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);// 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 30Fields 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 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 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
}
}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
}
}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.0Keep 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;
}
}
}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() { ... }
}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) { ... }
}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;
}
}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;
}
}- Class Definition: How to create classes with fields and methods
- Constructors: How to initialize objects when they're created
- Object Creation: Using
newkeyword to create instances - Method Calls: Calling methods on objects to perform actions
- Field Access: Reading and modifying object data
- Object Independence: Understanding that each object has its own data
- Input Validation: Checking parameters in constructors and methods
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()
// Wrong
Person person = Person("Alice", 25);
// Right
Person person = new Person("Alice", 25);// Person is the CLASS (blueprint)
class Person { ... }
// person1 is an OBJECT (instance)
Person person1 = new Person("Alice", 25);class Person {
String name;
public Person(String name) {
name = name; // Wrong! Sets parameter to itself
this.name = name; // Right! Sets object's field
}
}// Less secure
class BankAccount {
public double balance; // Anyone can modify directly
}
// More secure
class BankAccount {
private double balance; // Protected
public double getBalance() { ... } // Controlled access
}- Think About Real-World Objects: What data would they have? What can they do?
- Start Simple: Begin with basic fields and methods, add complexity gradually
- Use Meaningful Names: Class names should be nouns, method names should be verbs
- Test Your Objects: Create instances and call methods to verify behavior
- One Thing at a Time: Each class should represent one type of thing
- Plan Before Coding: Think about what fields and methods you need
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!