← Back to Materials

📜 Code Examples

All pseudocode from the lecture slides — organized by chapter

📚 6 Lectures
📝 8 Examples
🎯 Covering Object-Oriented to Relational Mapping
L1-L3

Object-Oriented Fundamentals — Classes

2 examples

Basic Class Definition (Java-like structure)

Slide 35 Definition
// Defining a basic Blueprint
class Employee {
    // Attributes (State)
    private String name;
    private double salary;

    // Constructor
    public Employee(String name, double salary) {
        this.name = name;
        this.salary = salary;
    }

    // Methods (Behavior)
    public void giveRaise(double percentage) {
        this.salary += this.salary * percentage;
    }
}
A class encapsulates both the data structure (attributes) and the logic (methods) that govern that data.

Creating Objects (Instantiation)

Slide 36 Instantiation
// In Memory execution
Employee emp1 = new Employee("Alice", 50000);
Employee emp2 = new Employee("Bob", 65000);

// Modifying state via encapsulated methods
emp1.giveRaise(0.10);  // Alice gets a 10% raise
Objects are the runtime instances of the Class blueprint. Each holds its own independent state.
L4-L5

Implementing Associations

2 examples

1-to-N Association (Object Model)

Slide 42 Object Model
class Department {
    private String name;
    // Implementation of 'Many' using a Collection
    private List<Employee> employees = new ArrayList<>();

    public void addEmployee(Employee e) {
        this.employees.add(e);
        // Depending on the persistence framework, 
        // the Employee object might also securely link back to the Department.
    }
}
In memory, associations are typically handled by collection instances embedded directly within the parent object.

Many-to-Many M:N Association

Slide 48 Association Class
// Without an Association Class:
class Student { List<Course> courses; }
class Course { List<Student> students; }

// WITH an Association Class (Handling extra attributes like 'Grade')
class Enrollment {
    private Student student;
    private Course course;
    private String grade;

    public Enrollment(Student s, Course c, String g) {
        this.student = s;
        this.course = c;
        this.grade = g;
    }
}
Association classes become necessary when the relationship itself needs to hold data (like a Grade given to a Student in a specific Course).
L9-L11

Relational Model Mapping (SQL)

4 examples

1-to-N Mapping using Foreign Keys

Slide 118 SQL DDL
-- The "One" side
CREATE TABLE Department (
    id INT PRIMARY KEY,
    name VARCHAR(50)
);

-- The "Many" side holds the Foreign Key pairing it to the parent.
CREATE TABLE Employee (
    id INT PRIMARY KEY,
    name VARCHAR(50),
    department_id INT,
    FOREIGN KEY (department_id) REFERENCES Department(id)
);
In relational databases, collections are NOT stored inside rows. Instead, child rows hold a Foreign Key to the parent's Primary Key.

M:N Mapping using Junction Tables

Slide 125 SQL Junction Table
-- Base Tables
CREATE TABLE Student (id INT PRIMARY KEY, name VARCHAR(50));
CREATE TABLE Course (id INT PRIMARY KEY, title VARCHAR(50));

-- Junction Table bridging the M:N gap
CREATE TABLE Enrollment (
    student_id INT,
    course_id INT,
    grade VARCHAR(2),
    PRIMARY KEY (student_id, course_id),
    FOREIGN KEY (student_id) REFERENCES Student(id),
    FOREIGN KEY (course_id) REFERENCES Course(id)
);
A pure many-to-many relationship cannot be represented directly in SQL without an intermediate junction table consisting of multiple Foreign Keys.

Class Inheritance: Single Table Strategy

Slide 132 ORM Pattern
-- Putting all parent and child attributes into one wide table.
CREATE TABLE Vehicle (
    id INT PRIMARY KEY,
    type VARCHAR(20),      -- Discriminator Column ('CAR' or 'TRUCK')
    top_speed INT,         -- Parent attribute
    num_doors INT,         -- 'CAR' only attribute (Nullable)
    cargo_capacity INT     -- 'TRUCK' only attribute (Nullable)
);
All subclasses mapped to one table. Requires a discriminator column. Subclass-specific columns must be nullable. Efficient for polymorphic queries but breaks normalization.

Class Inheritance: Table Per Class Strategy

Slide 135 ORM Pattern
-- Parent table holds generic data
CREATE TABLE Vehicle (
    id INT PRIMARY KEY, 
    top_speed INT
);

-- Child table holds specific data AND links back to Parent PK (as its own PK)
CREATE TABLE Car (
    vehicle_id INT PRIMARY KEY, 
    num_doors INT,
    FOREIGN KEY (vehicle_id) REFERENCES Vehicle(id)
);
Clean, highly normalized DB structure. However, fetching a full Car object requires a SQL JOIN operation between the Vehicle and Car tables.