Introduction to Object-Oriented Programming
Lecture 1
Understanding OOP concepts, classes, objects, and their relationships
2026 WayUp
By the end of this course, you will be able to:
| Module | Topics |
|---|---|
| 1. OOP Foundations | Objects, classes, UML class diagrams, relationships |
| 2. UML Behavioral | Use Case, Activity, Sequence diagrams |
| 3. Java Basics | IDE setup, syntax, types, encapsulation |
| 4. Control Flow | JVM, conditionals, loops, recursion |
| 5. OOP in Java | Inheritance, interfaces, polymorphism |
| 6. Data & Collections | Arrays, collections, exceptions, files |
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:
classDiagram
class Car {
-String color
-int speed
+accelerate()
+brake()
}
class myCar["myCar : Car"] {
color = "red"
speed = 60
}
class yourCar["yourCar : Car"] {
color = "blue"
speed = 0
}
class taxiCar["taxiCar : Car"] {
color = "yellow"
speed = 45
}
Car <|.. myCar : new
Car <|.. yourCar : new
Car <|.. taxiCar : new
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()
+run()
+eat(food)
+sleep()
}
Bundling data and methods together, hiding internal details
"A car hides its engine complexity behind simple pedals"Creating new classes based on existing ones
"A SportsCar is a Car with extra features"Same interface, different implementations
"All vehicles can move(), but each moves differently"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:
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)
classDiagram
class Person {
-String name
-String address
}
class Account {
-String accountNumber
-double balance
}
Person "1..2" -- "0..*" Account : owns
Aggregation means one class contains references to others, but they can exist independently:
classDiagram
class Email {
-String subject
-String body
-Date sentDate
}
class File {
-String filename
-int size
}
Email "1" o-- "0..*" File : attachments
Composition means strong ownership - the parts cannot exist without the whole:
classDiagram
class House {
-String address
-int floors
}
class Room {
-String name
-double area
}
House "1" *-- "1..*" Room : contains
Inheritance creates a hierarchy where specialized classes inherit from general ones:
classDiagram
class Mammal {
-boolean warmBlooded
+breathe()
+nurse()
}
class QuadrupedMammal {
-int legCount
+walk()
}
class Wolf {
-String packName
+howl()
+hunt()
}
class Dog {
-String breed
+bark()
+fetch()
}
Mammal <|-- QuadrupedMammal
QuadrupedMammal <|-- Wolf
QuadrupedMammal <|-- Dog
classDiagram
Animal <|-- Duck
Animal <|-- Fish
Animal <|-- Zebra
Animal : +int age
Animal : +String gender
Animal: +isMammal()
Animal: +mate()
class Duck{
+String beakColor
+swim()
+quack()
}
class Fish{
-int sizeInFeet
-canEat()
}
class Zebra{
+bool is_wild
+run()
}
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
class Customer {
-String name
-String address
+openAccount()
}
class Account {
-String accountNumber
-double balance
+deposit(amount)
+withdraw(amount)
}
class SavingsAccount {
-double interestRate
+calculateInterest()
}
class InvestmentAccount {
+buyStock(stock, qty)
+sellStock(stock, qty)
}
class Stock {
-String ticker
-double price
-int quantity
}
Customer "1" -- "0..*" Account
Account <|-- SavingsAccount
Account <|-- InvestmentAccount
InvestmentAccount "1" o-- "0..*" Stock
For each object, identify properties and behaviors:
| Object | Properties (State) | Methods (Behavior) |
|---|---|---|
| Smartphone | brand, model, batteryLevel, isOn | makeCall(), sendText(), charge() |
| BankAccount | ? | ? |
| Student | ? | ? |
| ShoppingCart | ? | ? |
Next: UML Behavioral Diagrams - Use Case, Activity, and Sequence diagrams to model system behavior.