Object-Oriented Programming
Discover the core concepts of Object-Oriented Programming (OOP) learn how classes and objects work, understand abstraction and encapsulation, and see how OOP helps model real-world problems efficiently using programming languages like Java and C++. Perfect for beginners aiming to build a strong foundation in modern software development.
Understanding Classes and Objects
What is a Class?
A class is a blueprint or template for creating objects.
It defines what data (attributes) and behaviors (methods) the object will have.
Think of a class as a plan, and an object as a real thing built from that plan.
Example (C++):
#include <iostream>
using namespace std;
class Car {
public:
string brand;
int speed;
void drive() {
cout << brand << " is driving at " << speed << " km/h" << endl;
}
};
int main() {
Car car1; // Object creation
car1.brand = "Toyota";
car1.speed = 120;
car1.drive(); // Output: Toyota is driving at 120 km/h
return 0;
}
Example (Java):
class Car {
String brand;
int speed;
void drive() {
System.out.println(brand + " is driving at " + speed + " km/h");
}
}
public class Main {
public static void main(String[] args) {
Car car1 = new Car();
car1.brand = "Honda";
car1.speed = 100;
car1.drive(); // Output: Honda is driving at 100 km/h
}
}
Explanation:
Caris a class that defines how all cars behave.car1is an object a real instance that has its own brand and speed.
Class → Blueprint
Object → Real car built from blueprint
Lecture 1 – Introduction to Programming Paradigms
Abstraction
Abstraction means hiding unnecessary details and showing only what is essential.
It allows the programmer to focus on what an object does, not how it does it.
Example:
When you drive a car, you use the steering wheel, brake, and accelerator
you don’t worry about how fuel turns into motion inside the engine.
That’s abstraction complex systems simplified through easy interfaces.
In Code (Java):
abstract class Animal {
abstract void makeSound(); // Abstract method — no body
}
class Dog extends Animal {
void makeSound() {
System.out.println("Woof Woof!");
}
}
public class Main {
public static void main(String[] args) {
Animal a = new Dog();
a.makeSound(); // Output: Woof Woof!
}
}
Explanation:
- We only know that every
Animalcan make a sound we don’t care how. - Each subclass provides its own sound.
- That’s abstraction focusing on the essential behavior, not the internal logic.
Encapsulation
Encapsulation means wrapping data and methods together in one unit (class).
It also helps in data hiding protecting internal data from being accessed directly.
Without Encapsulation (Wrong Way):
#include <iostream>
using namespace std;
class Student {
public:
string name;
int age;
};
int main() {
Student s;
s.age = -5; // Invalid data!
}
Anyone can directly change the data even incorrectly.
With Encapsulation (Correct Way):
#include <iostream>
using namespace std;
class Student {
private:
int age;
public:
void setAge(int a) {
if(a > 0)
age = a;
else
cout << "Invalid age!" << endl;
}
int getAge() {
return age;
}
};
int main() {
Student s;
s.setAge(20); // Valid
cout << "Age: " << s.getAge() << endl;
}
Explanation:
- Data (
age) is private, so no one can modify it directly. - Access is controlled through getters and setters.
- This ensures data integrity and safety.
How OOP Models Real-World Problems
OOP is inspired by real-world thinking.
Everything around us can be represented as objects each with data (attributes) and behavior (methods).
Example: School Management System
| Real-world Entity | Attributes | Behaviors |
|---|---|---|
| Student | name, rollNo, age | attendClass(), takeExam() |
| Teacher | name, subject, salary | teach(), evaluate() |
| Course | courseName, credits | enrollStudent(), assignTeacher() |
Code Example:
class Student {
String name;
int rollNo;
void attendClass() {
System.out.println(name + " is attending class.");
}
}
This mirrors how we think in real life each object has responsibilities.
Summary of Lecture 2
| Concept | Meaning | Example |
|---|---|---|
| Class | Blueprint or template | class Car { } |
| Object | Real instance of class | Car c = new Car(); |
| Abstraction | Hiding unnecessary details | abstract class Animal |
| Encapsulation | Data hiding via access control | private + getters/setters |
| Real-world Modeling | Represent entities as objects | Student, Teacher, Course |
Positive Thought for Students
“The best way to understand objects is to build them line by line, concept by concept. Every class you create is a new step toward thinking like a real developer!”




