Chapter 8: Introduction to OOPs
What is Object Oriented Programming?
Object Oriented programming tries to map code instructions with real world making the code short and easier to understand.
Solving a problem by creating objects is one of the most popular approaches in programming. This is called Object Oriented Programming.
🔄 Code Reusability
Write once, use multiple times through inheritance
🛡️ Data Security
Encapsulation protects data from unauthorized access
🧩 Modularity
Break complex problems into smaller, manageable parts
What is DRY?
DRY stands for - Do not repeat yourself
It focuses on Code reusability
OOPs Terminology
1. Abstraction
Hiding internal details (show only essential info)
📱 You use this phone without bothering about how it was made.
2. Encapsulation
The act of putting various components together (in a capsule)
💻 Laptop is a single entity with wifi + Speaker + Storage in a single box!
In Java, encapsulation means that sensitive data can be hidden from users.
3. Inheritance
The act of deriving new things from existing things
🛺 Rickshaw ⇒ E-Rickshaw
📞 Phone ⇒ Smart Phone
Implements DRY!
4. Polymorphism
One entity many forms
📱 Smartphone → Phone
📱 Smartphone → Calculator
Classes and Objects
Class
A class is a blueprint for creating objects.
Contains info to create a valid object.
Object
An Object is an instantiation of a class. When a class is defined, a template (info) is defined. Memory is allocated only after object instantiation.
Class vs Object Analogy
📋 JEE Application Form
(Class - Blueprint)
✍️ Filled by Student
(Object Creation Process)
📄 Application for that Student
(Object - Instance)
How to Model a Problem in OOPs
We identify the following:
Example: Employee Management System
| Component | Type | Examples |
|---|---|---|
| Employee | Class (Noun) | The main entity |
| name, age, salary | Attributes (Adjectives) | Properties of employee |
| getSalary(), increment() | Methods (Verbs) | Actions employee can perform |
Remember: Any real world Object = Properties + Behaviour
Object in OOPs = Attributes + Methods
Writing a Custom Class
Basic Class Structure
public class Employee {
int id; // Attribute 1
String name; // Attribute 2
}
Class with Methods
public class Employee {
public int id;
public String name;
public int salary;
public int getSalary() {
return salary;
}
public void getDetails() {
System.out.println("Employee ID: " + id);
System.out.println("Employee Name: " + name);
System.out.println("Employee Salary: " + salary);
}
public void setName(String newName) {
name = newName;
}
}
Using the Employee Class
public class EmployeeTest {
public static void main(String[] args) {
// Creating objects of Employee class
Employee emp1 = new Employee();
Employee emp2 = new Employee();
// Setting attributes for emp1
emp1.id = 101;
emp1.name = "John Doe";
emp1.salary = 50000;
// Setting attributes for emp2
emp2.id = 102;
emp2.name = "Jane Smith";
emp2.salary = 60000;
// Using methods
System.out.println("Employee 1 Details:");
emp1.getDetails();
System.out.println("\nEmployee 2 Details:");
emp2.getDetails();
// Modifying employee name
emp1.setName("John Updated");
System.out.println("\nAfter name update:");
emp1.getDetails();
// Getting salary
System.out.println("\nEmployee 1 Salary: " + emp1.getSalary());
}
}
Practice Set
Chapter 8 - Practice Set
1. Employee Class
Create a class Employee with following properties and methods:
• Salary (property) (int)
• getSalary (method returning int)
• name (property) (String)
• getName (method returning String)
• setName (method changing name)
2. CellPhone Class
Create a class CellPhone with methods to print "ringing...", "vibrating..." etc.
3. Square Class
Create a class Square with a method to initialize its side, calculating area, perimeter etc.
4. Rectangle Class
Create a class Rectangle & repeat problem 3.
5. Tommy Vercetti Class
Create a class Tommy Vercetti for Rockstar Games capable of hitting (print hitting...), running, firing etc.
6. Circle Class
Repeat problem 4 for a Circle.