Unit 3.3: Anatomy of a Class
A class is a formal blueprint used to create objects. It defines the data (attributes) and behaviors (methods) that every object of that class will possess.
Data Encapsulation
Think of a class like a bank vault. The money inside is hidden and protected.
- Encapsulation bundles data (attributes) and methods into a single unit (class) and restricts direct access to some of the object's components. This protects the internal state and prevents illegal modifications.
Data encapsulation is the technique of hiding the "inside" parts of your code from the "outside" world.
We use two keywords to control this:
private: Only code inside the same class can see or change this. (This is the standard for data).public: Any class anywhere can see or use this.
public class SecretBox {
private String secret; // Standard: Hidden and safe
public String label; // WARNING: Not preferred. Most data should be private!
}
Class Declaration
A class is the "blueprint." In this course, every class we create will be marked as public. This ensures that the class is "visible" and can be used by any other class in our entire program.
// How we start every class in AP CSA
public class MyProgram {
// Everything goes inside these curly braces
}
- In professional Java, classes can also be "package-private" (by leaving out the word
public). However, this is not part of the AP CSA course. You should always usepublicfor your classes.
Constructor Visibility
A constructor is the code that actually "builds" the object from the blueprint. We make constructors public so that other classes are allowed to create objects of this type.
public class Robot {
// Public so we can say 'new Robot()' in another file
public Robot() {
System.out.println("Robot created!");
}
}
- In professional Java, constructors can also be marked as
privateorprotected. However, for the purposes of the AP CSA course, constructors are always designated aspublic.
Instance Variables
An instance variable is a piece of data that belongs to a specific object.
- "Instance" is just a fancy word for "individual object."
- If you have three
Dogobjects, each one gets its own copy of thenamevariable.
public class Dog {
private String name; // Each dog has its own unique name
public static void main(String[] args) {
Dog d1 = new Dog(); // This dog has its own name copy
Dog d2 = new Dog(); // This dog has a different name copy
}
}
Protecting State with private
Why use private? It prevents "accidents." If your data is public, anyone can reach in and change it to something impossible (like setting a person's age to -500). By making data private, we force people to use our methods, where we can check if the data is valid first.
public class Person {
private int age; // Private protects the value from outside tampering
// We use a public method to change the data safely
public void setAge(int newAge) {
if (newAge >= 0) {
this.age = newAge; // Only set if the value makes sense!
} else {
System.out.println("Error: Age cannot be negative.");
}
}
}
Access to Behaviors (Methods)
Methods are the "actions" or "buttons" of an object.
- Public Methods: The buttons anyone can press. These form the "face" of your class.
- Private Methods: Internal "helper" steps that the object does behind the scenes.
public class Smartphone {
// Anyone can press the "Call" button
public void makeCall() {
connectToNetwork(); // Hidden internal step
System.out.println("Ringing...");
}
// This step is internal. The user doesn't need to see it.
private void connectToNetwork() {
// Complex technical logic here
}
}
Examples
Complete Class Structure: BankAccount
This example illustrates how all the components of a class anatomy fit together.
// Classes are always public in this course
public class BankAccount {
// 1. Attributes (Instance Variables) are designated as private
private String accountNumber;
private double balance;
// 2. Constructors are always designated as public
public BankAccount(String accountNumber) {
this.accountNumber = accountNumber;
this.balance = 0.0; // Initial state
}
// 3. Methods can be public (part of the external interface)
public double getBalance() {
return this.balance;
}
public void deposit(double amount) {
if (amount > 0) {
this.balance += amount;
logTransaction("Deposit", amount); // Calling a private method
}
}
// 4. Methods can be private (internal helper behavior)
private void logTransaction(String type, double amount) {
System.out.println("LOG: " + type + " of $" + amount);
}
}