Loading...
Loading...
Object-Oriented Programming (OOP) is a programming paradigm that organizes software design around objects — real-world entities that combine data (attributes) and behavior (methods).
OOP vs Procedural: | Feature | Procedural (C) | OOP (Java/C++) | |---|---|---| | Focus | Functions | Objects | | Data | Global/passed around | Encapsulated in objects | | Code reuse | Functions | Inheritance | | Security | Low | High (data hiding) | | Real-world modeling | Difficult | Natural |
Class = Blueprint/template Object = Instance of a class (actual entity)
// Class definition (Java)
public class Student {
// Attributes (fields)
private String name;
private int rollNo;
private double cgpa;
// Constructor
public Student(String name, int rollNo, double cgpa) {
this.name = name;
this.rollNo = rollNo;
this.cgpa = cgpa;
}
// Method
public void display() {
System.out.println(name + " | Roll: " + rollNo + " | CGPA: " + cgpa);
}
}
// Creating objects
Student s1 = new Student("Rahul", 101, 8.5);
Student s2 = new Student("Priya", 102, 9.0);
s1.display(); // Rahul | Roll: 101 | CGPA: 8.5
Constructors:
Encapsulation = Bundling data and methods + restricting direct access to data.
Implementation: Private fields + public getters/setters
public class BankAccount {
private double balance; // private — cannot access directly
public double getBalance() { return balance; } // getter
public void deposit(double amount) {
if (amount > 0) balance += amount; // validation inside
}
public boolean withdraw(double amount) {
if (amount <= balance) { balance -= amount; return true; }
return false;
}
}
BankAccount acc = new BankAccount();
acc.deposit(5000);
// acc.balance = 10000; // ERROR — private!
System.out.println(acc.getBalance()); // 5000.0
Benefits: Data validation, security, flexibility to change implementation.
Inheritance = Child class acquires properties and methods of parent class. Promotes code reuse.
// Parent class
class Animal {
String name;
void eat() { System.out.println(name + " is eating"); }
void sleep() { System.out.println(name + " is sleeping"); }
}
// Child class
class Dog extends Animal {
void bark() { System.out.println(name + " says: Woof!"); }
}
Dog d = new Dog();
d.name = "Tommy";
d.eat(); // Inherited from Animal
d.bark(); // Dog's own method
Types of Inheritance: | Type | Description | Java Support | |---|---|---| | Single | A → B | Yes | | Multilevel | A → B → C | Yes | | Hierarchical | A → B, A → C | Yes | | Multiple | A,B → C | No (use interface) | | Hybrid | Combination | No (use interface) |
super keyword: Access parent class members
class Child extends Parent {
Child() {
super(); // Call parent constructor
super.display(); // Call parent method
}
}
Polymorphism = One name, many forms.
class Calculator {
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
int add(int a, int b, int c) { return a + b + c; }
// Same name, different parameters
}
class Shape {
void draw() { System.out.println("Drawing a shape"); }
}
class Circle extends Shape {
@Override
void draw() { System.out.println("Drawing a circle"); }
}
class Rectangle extends Shape {
@Override
void draw() { System.out.println("Drawing a rectangle"); }
}
Shape s = new Circle();
s.draw(); // Output: Drawing a circle (decided at runtime!)
Dynamic Method Dispatch: Parent reference → child object → child method called.
Abstraction = Hiding implementation details, showing only essential features.
abstract class Vehicle {
abstract void start(); // abstract — no body
void refuel() { System.out.println("Refueling..."); } // concrete method
}
class Car extends Vehicle {
@Override
void start() { System.out.println("Car started with key"); }
}
interface Drawable {
void draw(); // implicitly public abstract
default void show() { System.out.println("Showing..."); } // Java 8+
}
class Circle implements Drawable {
public void draw() { System.out.println("Drawing circle"); }
}
// Multiple interfaces
class Artist implements Drawable, Printable { ... }
Abstract Class vs Interface: | Feature | Abstract Class | Interface | |---|---|---| | Methods | Abstract + concrete | Abstract (+ default in Java 8+) | | Variables | Any type | public static final only | | Constructor | Yes | No | | Multiple inheritance | No | Yes | | Use when | IS-A relationship | CAN-DO capability |
try {
int result = 10 / 0; // throws ArithmeticException
} catch (ArithmeticException e) {
System.out.println("Error: " + e.getMessage());
} catch (Exception e) {
System.out.println("General error");
} finally {
System.out.println("This always executes");
}
Custom Exception:
class InsufficientFundsException extends Exception {
InsufficientFundsException(String msg) { super(msg); }
}
Q1 (2023): Explain with example why Java does not support multiple inheritance. Java avoids the "Diamond Problem" — if class C inherits from both A and B which have same method, Java cannot determine which to use. Solution: Use interfaces (multiple interface implementation is allowed).
Q2 (2023): What is the output?
class A { void show() { System.out.println("A"); } }
class B extends A { void show() { System.out.println("B"); } }
A obj = new B();
obj.show();
Output: B — Runtime polymorphism; type of actual object (B) determines method called.
Q3 (2022): Difference between constructor and method. Constructor: same name as class, no return type, called automatically on object creation. Method: different name possible, has return type, called explicitly.
Complete OOP notes for B.Tech CS Semester 3 — classes, objects, inheritance, polymorphism, encapsulation, abstraction with Java and C++ examples and solved PYQs.
50 pages · 2.5 MB · Updated 2026-03-11
Encapsulation (data hiding), Inheritance (code reuse), Polymorphism (many forms), Abstraction (hiding complexity). Remember: EIPA or A-PIE.
Abstract class can have method implementations + abstract methods. Interface (pre-Java 8) had only abstract methods. A class can implement multiple interfaces but extend only one abstract class.
Overloading: same method name, different parameters (compile-time polymorphism). Overriding: child class redefines parent method with same signature (runtime polymorphism).
Algorithms — Design, Analysis, and Techniques
Algorithms
Data Structures Complete Notes — B.Tech CS Sem 3
Data Structures
DBMS Complete Notes — B.Tech CS Sem 4
Database Management Systems
Compiler Design — Complete Notes CS Sem 6
Compiler Design
Machine Learning Complete Notes — B.Tech CS Sem 6
Machine Learning
Your feedback helps us improve notes and tutorials.