Chapter 8 - Classes - Study Guide
Chapter 8 - Classes - Study Guide
Overview
This chapter introduces object-oriented programming (OOP) concepts in Java, focusing on creating
and using classes to define new types of objects that combine state (data) and behavior (methods).
Key Concepts and Definitions
1. Object-Oriented Programming Fundamentals
Object: An entity that combines state and behavior
Definition: A specific instance of a class that contains its own data and can perform actions
Example: If you have a Point class, then Point p1 = new Point(5, 3); creates an object
p1 with coordinates (5, 3)
public void translate(int theDx, int theDy) { // behavior objects will have
myX += theDx;
myY += theDy;
}
}
2. Module Class
Definition: A class containing utility methods that don't require object creation
Characteristics: All methods are static, used by other classes
Example:
java
Example:
java
Constructors
Definition and Examples
Constructor: A special method that initializes the state of new objects
Definition: Code that runs when creating a new object with the new keyword
Key Points: No return type, same name as class, can have parameters
Basic Constructor Syntax:
java
public ClassName(parameters) {
// initialization statements
}
Constructor Examples:
java
// Usage examples:
Point p1 = new Point(5, 3); // Uses first constructor
Point p2 = new Point(); // Uses default constructor
// CORRECT:
public Point(int theX, int theY) {
myX = theX;
myY = theY;
}
Methods
Instance Methods
Instance Method: A method that exists inside each object and operates on that object's data
Definition: Methods that belong to objects and can access the object's fields directly
Key Point: Each object has its own copy of the method that works with that object's data
Syntax:
java
// Usage:
Point p = new Point(3, 4);
System.out.println("X coordinate: " + p.getX()); // 3
System.out.println("Distance from origin: " + p.distanceFromOrigin()); // 5.0
// Usage:
Point p = new Point(5, 3);
p.setX(10); // Changes x to 10
p.translate(2, 1); // Moves point by (2, 1)
p.setLocation(0, 0); // Moves point to origin
With toString():
java
// Usage:
Point p = new Point(10, 7);
System.out.println("Point: " + p); // Output: Point: (10, 7)
System.out.println(p.toString()); // Output: (10, 7)
// Usage:
Student s = new Student("Alice", 3.8, 20);
System.out.println(s); // Output: Alice (age 20, GPA: 3.8)
Encapsulation
Definition and Examples
Encapsulation: Hiding implementation details from clients
Definition: Making fields private and providing controlled access through methods
Purpose: Protects object data and allows internal changes without affecting clients
Private Fields:
java
Benefits Example:
java
// Accessors (getters)
public String getName() {
return myName;
}
Constructor Chaining:
java
public Point() {
this(0, 0); // Calls the other constructor
}
Static Members
Static Fields
Static Field: A field that belongs to the class rather than to individual objects
Definition: Shared by all objects of the class; only one copy exists
Use Case: Data that should be shared across all instances
Example:
java
// Usage:
Student s1 = new Student("Alice"); // ourStudentCount becomes 1
Student s2 = new Student("Bob"); // ourStudentCount becomes 2
System.out.println(Student.getStudentCount()); // Output: 2
System.out.println(s1.getId()); // Output: 1
System.out.println(s2.getId()); // Output: 2
Static Methods
Static Method: A method that belongs to the class rather than to individual objects
Definition: Cannot access instance fields or methods directly; no implicit parameter
Use Case: Utility methods that don't need object data
Example:
java
// Creating objects
points[0] = new Point(1, 2);
points[1] = new Point(3, 4);
// points[2] is still null
// Safe access
for (int i = 0; i < points.length; i++) {
if (points[i] != null) {
System.out.println(points[i].toString());
}
}
// Safe approach:
if (words[0] != null) {
System.out.println(words[0].length());
}
Two-Phase Initialization:
java
// Constructors
public Point(int theX, int theY) {
myX = theX;
myY = theY;
}
public Point() {
this(0, 0); // Default to origin
}
// Accessors
public int getX() {
return myX;
}
// Mutators
public void setLocation(int theX, int theY) {
myX = theX;
myY = theY;
}
// Constructor
public BankAccount(String theOwner, double theInitialBalance) {
myOwner = theOwner;
myBalance = theInitialBalance;
myAccountNumber = ourNextAccountNumber;
ourNextAccountNumber++;
}
// Accessors
public double getBalance() {
return myBalance;
}
// Static method
public static int getNextAccountNumber() {
return ourNextAccountNumber;
}
// Mutators
public void deposit(double theAmount) {
if (theAmount > 0) {
myBalance += theAmount;
}
}
// toString
public String toString() {
return "Account #" + myAccountNumber + " (" + myOwner + "): $" + myBalance;
}
}
// Usage example:
BankAccount account1 = new BankAccount("Alice", 500.0); // Account #1000
BankAccount account2 = new BankAccount("Bob", 300.0); // Account #1001
System.out.println(account1); // Account #1000 (Alice): $500.0
account1.deposit(100);
account1.withdraw(50);
System.out.println(account1); // Account #1000 (Alice): $550.0