☕ Object Oriented Programming using Java (105303)¶
⬅️ Back to Semester-3 | 🏠 Home
💡 Why this subject? OOP is how almost all real-world large software (Android apps, enterprise systems, games) is structured. Java forces you to learn it the disciplined way.
📌 Unit 1: OOP Concepts and Java Basics¶
- Procedural vs OOP: procedural = functions act on data separately; OOP = data + functions bundled together as objects.
- 4 Pillars of OOP: Encapsulation, Inheritance, Polymorphism, Abstraction.
- JDK vs JRE vs JVM:
- JVM: runs compiled Java bytecode (makes Java "write once, run anywhere").
- JRE: JVM + libraries needed to run Java programs.
- JDK: JRE + compiler & tools needed to develop Java programs.
public class Hello {
public static void main(String[] args) {
int x = 10;
for (int i = 0; i < x; i++) {
System.out.println("i = " + i);
}
}
}
🧠 Quick Recall: Source .java → compiled to .class (bytecode) by javac → run by java (JVM interprets/JIT-compiles it).
📌 Unit 2: Objects, Classes, and Constructors¶
class Student {
String name;
int roll;
// Constructor
Student(String name, int roll) {
this.name = name; // 'this' refers to current object
this.roll = roll;
}
// Constructor overloading
Student() {
this("Unknown", 0); // calls the other constructor
}
void display() {
System.out.println(name + " - " + roll);
}
static int totalStudents = 0; // static = shared by ALL objects
}
public class Main {
public static void main(String[] args) {
Student s1 = new Student("Pratap", 101);
s1.display();
}
}
- Access Modifiers:
public(anywhere),private(same class only),protected(same package + subclasses), default (same package only). - Garbage Collection: Java automatically frees memory of objects no longer referenced — no
free()/deleteneeded like in C.
🧠 Quick Recall: private fields + public getter/setter methods = Encapsulation in action.
📌 Unit 3: Inheritance, Interfaces and Packages¶
class Animal {
void eat() { System.out.println("Eating..."); }
}
class Dog extends Animal { // inheritance
void eat() { // method overriding
System.out.println("Dog eats bones");
}
void bark() { System.out.println("Woof!"); }
}
interface Vehicle {
void start(); // abstract by default
}
class Car implements Vehicle {
public void start() { System.out.println("Car starting..."); }
}
- Abstract class vs Interface:
- Abstract class: can have both implemented & unimplemented methods, supports single inheritance.
- Interface: (traditionally) all methods abstract, a class can implement multiple interfaces — Java's way around "no multiple inheritance."
- Polymorphism:
- Compile-time (Method Overloading): same method name, different parameters.
- Runtime (Method Overriding): subclass redefines a parent method, decided at runtime based on actual object type.
📝 Example — Polymorphism in action:
Animal a = new Dog(); // Animal reference, Dog object
a.eat(); // prints "Dog eats bones" — runtime polymorphism!
🧠 Quick Recall: "Why can't I create object of an interface?" → because it has no implementation, only a contract.
📌 Unit 4: Exception Handling¶
try {
int result = 10 / 0; // throws ArithmeticException
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero: " + e.getMessage());
} finally {
System.out.println("This always runs");
}
// Custom exception
class InvalidAgeException extends Exception {
InvalidAgeException(String msg) { super(msg); }
}
void checkAge(int age) throws InvalidAgeException {
if (age < 18) throw new InvalidAgeException("Age must be 18+");
}
- Checked exceptions: must be declared/handled (e.g.,
IOException) — compiler enforces it. - Unchecked exceptions: runtime errors (e.g.,
NullPointerException) — compiler doesn't force handling.
🧠 Quick Recall: finally block always executes — even if there's a return in try/catch — used for cleanup (closing files/connections).
📌 Unit 5: Multithreading¶
class MyThread extends Thread {
public void run() {
System.out.println("Thread running: " + Thread.currentThread().getName());
}
}
public class Main {
public static void main(String[] args) {
MyThread t1 = new MyThread();
t1.start(); // NOT t1.run() — start() creates a new thread
}
}
// Synchronization to prevent race conditions
synchronized void updateBalance() {
// only one thread can execute this at a time
}
🧠 Quick Recall: start() actually spawns a new thread of execution; calling run() directly just runs it on the current thread like a normal method — a very common interview trap question!
📌 Unit 6: Files, Collections Framework & JDBC¶
// File writing
import java.io.*;
FileWriter fw = new FileWriter("data.txt");
fw.write("Hello Java File!");
fw.close();
// Collections Framework
import java.util.*;
ArrayList<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
HashSet<Integer> set = new HashSet<>(); // no duplicates
set.add(10); set.add(10); // only stored once
HashMap<String, Integer> map = new HashMap<>();
map.put("Pratap", 90);
System.out.println(map.get("Pratap"));
Collections hierarchy (quick view):
Collection
├── List (ordered, duplicates allowed) → ArrayList, LinkedList
├── Set (no duplicates) → HashSet, TreeSet
└── Queue (FIFO) → PriorityQueue, ArrayDeque
- JDBC (Java Database Connectivity): lets Java programs connect & run SQL queries on a database.
🔬 Lab Highlights (Java Lab)¶
- Basic syntax & control structures
- Classes, Objects, Methods, Constructors
- Method & Constructor Overloading
- Inheritance +
superkeyword - Method Overriding & runtime polymorphism
- Abstract class & Interface
- User-defined exceptions
- Multithreading with synchronization
- File handling (byte & character streams)
- Collections Framework & JDBC connectivity
✅ Quick Revision Table¶
| Topic | One-line memory hook |
|---|---|
| JVM/JRE/JDK | Run / Run+Libraries / Develop+Run+Libraries |
| Encapsulation | private fields + public getters/setters |
| Overloading | Same name, different params, decided at compile-time |
| Overriding | Subclass redefines method, decided at runtime |
| Interface | 100% contract, supports "multiple inheritance" |
finally |
Always runs — used for cleanup |
start() vs run() |
start() = new thread, run() = normal method call |
| ArrayList vs HashSet | Ordered+duplicates vs unordered+unique |