Understanding OOP concepts, classes, objects, and their relationships
By the end of this course, you will be able to:
| Module | Topics |
|---|---|
| 1. OOP Foundations | Objects, classes, UML diagrams, relationships |
| 2. Java Basics | Syntax, types, IDE setup, compilation |
| 3. Control Flow | Conditionals, loops, recursion |
| 4. OOP in Java | Inheritance, interfaces, polymorphism |
| 5. Data Handling | Collections, files, exceptions |
| 6. Advanced Topics | Design patterns, functional programming, GUI |
Before OOP, programs were written as sequences of procedures (functions):
// Procedural approach - data and functions are separate
String[] employeeNames = {"Alice", "Bob", "Charlie"};
double[] employeeSalaries = {50000, 60000, 55000};
String[] employeeDepartments = {"IT", "HR", "IT"};
void giveRaise(int employeeIndex, double amount) {
employeeSalaries[employeeIndex] += amount;
}
void transferDepartment(int employeeIndex, String newDept) {
employeeDepartments[employeeIndex] = newDept;
}
// Problems:
// - Data is scattered across multiple arrays
// - Easy to mix up indices
// - Hard to add new employee attributes
// - No way to enforce rules (e.g., salary can't be negative)
OOP organizes code around objects that combine data and behavior:
// Object-Oriented approach - data and behavior together
class Employee {
String name;
double salary;
String department;
void giveRaise(double amount) {
if (amount > 0) { // Can enforce rules!
this.salary += amount;
}
}
void transferTo(String newDepartment) {
this.department = newDepartment;
}
}
// Usage is intuitive:
Employee alice = new Employee("Alice", 50000, "IT");
alice.giveRaise(5000);
alice.transferTo("Management");
Car object has properties (color, speed) and behaviors (accelerate, brake)Employee class, create thousands of employeesAn object is a thing that has:
flowchart LR
subgraph Blueprint["π CLASS: Car"]
direction TB
Props["String color
int speed"]
Methods["accelerate()
brake()"]
end
Blueprint -->|new Car| myCar
Blueprint -->|new Car| yourCar
Blueprint -->|new Car| taxiCar
subgraph Instances["π OBJECTS (Instances)"]
myCar["myCar
βββββββββ
color: red
speed: 60"]
yourCar["yourCar
βββββββββ
color: blue
speed: 0"]
taxiCar["taxiCar
βββββββββ
color: yellow
speed: 45"]
end
style Blueprint fill:#e8f4f8,stroke:#3498db,stroke-width:2px
style Instances fill:#fef5e7,stroke:#e67e22,stroke-width:2px
style myCar fill:#fff,stroke:#e74c3c
style yourCar fill:#fff,stroke:#3498db
style taxiCar fill:#fff,stroke:#f1c40f
Properties describe the state of an object - what it "has" or "is":
classDiagram
class Dog {
-String name
-int age
-String breed
-String color
}
Methods define what an object can do - its behaviors:
classDiagram
class Dog {
-String name
-int age
-String breed
+bark() void
+run() void
+eat(food) void
+sleep() void
}
|
Encapsulation Bundling data and methods together, hiding internal details "A car hides its engine complexity behind simple pedals" |
Inheritance Creating new classes based on existing ones "A SportsCar is a Car with extra features" |
|
Polymorphism Same interface, different implementations "All vehicles can move(), but each moves differently" |
Abstraction Showing only essential features, hiding complexity "You use a TV remote without knowing electronics" |
Before coding, we need to design and communicate our ideas:
A class is represented as a box with three sections:
Objects don't exist in isolation - they interact and relate to each other:
| Relationship | Meaning | Example |
|---|---|---|
| Association | "uses" or "knows about" | Student enrolls in Course |
| Aggregation | "has" (weak ownership) | Playlist has Songs |
| Composition | "owns" (strong ownership) | House has Rooms |
| Inheritance | "is a" (specialization) | Dog is an Animal |
An association shows that two classes are connected:
classDiagram
direction LR
class Person {
-String name
-String address
}
class Account {
-String accountNumber
-double balance
}
Person "1..2" -- "0..*" Account : owns
Cardinality indicates how many objects can be involved:
1 - Exactly one0..1 - Zero or one (optional)* or 0..* - Zero or more1..* - One or more1..2 - One to two (as shown: max 2 owners per account)Aggregation means one class contains references to others, but they can exist independently:
classDiagram
direction LR
class Email {
-String subject
-String body
-Date sentDate
}
class File {
-String filename
-int size
}
Email o-- "0..*" File : attachments
Composition means strong ownership - the parts cannot exist without the whole:
classDiagram
direction LR
class House {
-String address
-int floors
}
class Room {
-String name
-double area
}
House *-- "1..*" Room : contains
Inheritance creates a hierarchy where specialized classes inherit from general ones:
classDiagram
direction TB
class Mammal {
-boolean warmBlooded
+breathe() void
+nurse() void
}
class QuadrupedMammal {
-int legCount
+walk() void
}
class Wolf {
-String packName
+howl() void
+hunt() void
}
class Dog {
-String breed
+bark() void
+fetch() void
}
Mammal <|-- QuadrupedMammal
QuadrupedMammal <|-- Wolf
QuadrupedMammal <|-- Dog
When you specialize, the child class:
Consider a Library Management System:
Questions:
| Class | Properties | Methods |
|---|---|---|
| Book | title, author, ISBN, isAvailable | reserve(), checkOut(), return() |
| Member | name, address, cardNumber | borrowBook(), returnBook(), reserveBook() |
| Librarian | name, employeeId | processLoan(), processReturn(), addBook() |
| Loan | book, member, loanDate, dueDate | extend(), calculateFine() |
Relationships:
Design a class diagram for a banking system:
Questions:
classDiagram
direction TB
class Customer {
-String name
-String address
+openAccount() Account
}
class Account {
-String accountNumber
-double balance
+deposit(amount) void
+withdraw(amount) void
}
class SavingsAccount {
-double interestRate
+calculateInterest() double
}
class InvestmentAccount {
+buyStock(stock, qty) void
+sellStock(stock, qty) void
}
class Stock {
-String ticker
-double price
-int quantity
}
Customer "1" -- "0..*" Account : owns
Account <|-- SavingsAccount
Account <|-- InvestmentAccount
InvestmentAccount o-- "0..*" Stock : holds
For each object, identify properties and behaviors:
| Object | Properties (State) | Methods (Behavior) |
|---|---|---|
| Smartphone | brand, model, batteryLevel, isOn | makeCall(), sendText(), charge() |
| BankAccount | ? | ? |
| Student | ? | ? |
| ShoppingCart | ? | ? |
We'll use IntelliJ IDEA, a powerful Java IDE:
Next: We'll start coding in Java and bring these concepts to life!