OOP & Class Design Lab
Build Java classes with fields, constructors, and methods. Create objects, trace method calls step by step, and explore core object-oriented programming principles through a live UML class diagram.
Guided Experiment: Encapsulation and Access Control
How do access modifiers (private, public, protected) control who can access fields and methods? Why is it beneficial to make fields private?
Write your hypothesis in the Lab Report panel, then click Next.
UML Class Diagram
Controls
public class Student {
private String name;
private double gpa;
public Student(String name, double gpa) {
this.name = name;
this.gpa = gpa;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public double getGpa() {
return this.gpa;
}
public void setGpa(double gpa) {
this.gpa = gpa;
}
public void enroll(String course) {
System.out.println(this.name + " enrolled in " + course);
}
@Override
public String toString() {
return "Student{" + "name=" + name + ", " + "gpa=" + gpa + "}";
}
}OOP Principles Demonstrated
- Encapsulation: Private fields with public getters/setters control access to internal state.
- Polymorphism: Overriding toString() provides class-specific behavior for an inherited method.
- Constructor initialization: Objects are initialized to a valid state via constructor parameters.
Data Table
(0 rows)| # | Class | Fields | Methods | Object State | Principle |
|---|
Reference Guide
Encapsulation & Access Modifiers
Encapsulation bundles data (fields) and the methods that operate on it into a single unit. Access modifiers control visibility.
public class Student {
private String name; // only this class
protected double gpa; // this + subclasses
public int id; // everyone
public String getName() {
return this.name; // controlled access
}
}
Making fields private and providing public
getters/setters lets you validate data before changes and keep
internal representation flexible.
Constructors & Initialization
A constructor runs when you create an object with new.
It sets the initial state so the object is valid from the start.
public class BankAccount {
private double balance;
public BankAccount(double balance) {
this.balance = balance;
}
// Usage:
// BankAccount acct = new BankAccount(500.0);
}
The this keyword distinguishes the field from the
parameter when they share the same name.
Inheritance & Polymorphism
A child class extends a parent to inherit its fields
and methods. Override a method to give it specialized behavior.
public class Circle extends Shape {
private double radius;
public Circle(String color, double r) {
super(color); // call parent constructor
this.radius = r;
}
@Override
public double getArea() {
return Math.PI * radius * radius;
}
}
Polymorphism means a Shape variable can hold a
Circle, and calling getArea() runs the
Circle version.
Object State & References
Each object has its own copy of instance fields. Method calls can read or change those fields. Two variables can reference the same object.
BankAccount a = new BankAccount(100);
BankAccount b = a; // same object!
a.deposit(50);
System.out.println(b.getBalance());
// prints 150 — both reference one object Use the Object Tracer tab to step through method calls and watch how field values change after each call.