OOP java
Object-Oriented Programming - Detailed
Study Guide
1. Procedure Oriented Programming vs Object Oriented
Programming
Mnemonic: "DATA SAFE" — OOP keeps Data Safe, POP exposes it
Feature POP (Procedure Oriented) OOP (Object Oriented)
Focus Functions/Procedures Objects and Classes
Data Access Data is global, shared freely Data is encapsulated (hidden)
Approach Top-down Bottom-up
Data Security Less secure More secure
Reusability Limited High (via inheritance)
Real-world modeling Poor Excellent
Examples C, Pascal, FORTRAN Java, C++, Python
Modification Hard to modify Easy to modify
POP divides the program into small functions. All functions can access global data
freely — like a shared office where anyone touches anyone's files.
OOP divides the program into objects. Each object controls its own data — like
private lockers where only the owner has the key.
2. Access Control (In Detail)
Mnemonic: "PPD Pro" → Private, Protected, Default, Public
Access modifiers control who can see and use a class member (variable/method).
public
Accessible from everywhere — any class, any package.
OOP java 1
public int age = 25; // Anyone can access
private
Accessible only within the same class.
Most restrictive. Used to hide data (core of encapsulation).
private int salary; // Only accessible inside its own class
protected
Accessible within the same package + subclasses (even in different
packages).
protected String name; // Subclasses and same-package classes
can access
default (no modifier)
Accessible only within the same package.
When you write nothing, this applies automatically.
int marks; // Only classes in the same package can access
Summary Table
Modifier Same Class Same Package Subclass Other Package
public ✅ ✅ ✅ ✅
protected ✅ ✅ ✅ ❌
default ✅ ✅ ❌ ❌
private ✅ ❌ ❌ ❌
Mnemonic for most → least access: "Public People Don't Prefer Privacy" public
→ protected → default → private
OOP java 2
3. Inheritance — Types with Examples
Definition: Inheritance is the mechanism by which one class (child/subclass)
acquires the properties and behaviors of another class (parent/superclass). It
promotes code reusability.
Keyword used: extends
Mnemonic for types: "Single Mum Has Many Hybrids"
→ Single, Multilevel, Hierarchical, Multiple (interface), Hybrid
1. Single Inheritance
One child inherits from one parent.
class Animal {
void eat() { [Link]("Animal eats"); }
}
class Dog extends Animal {
void bark() { [Link]("Dog barks"); }
}
// Dog can eat() AND bark()
2. Multilevel Inheritance
A chain — Child becomes parent of another child.
class Animal { void eat() { } }
class Dog extends Animal { void bark() { } }
class Puppy extends Dog { void weep() { } }
// Puppy inherits from both Dog and Animal
3. Hierarchical Inheritance
One parent, multiple children.
OOP java 3
class Animal { void eat() { } }
class Dog extends Animal { void bark() { } }
class Cat extends Animal { void meow() { } }
// Both Dog and Cat inherit from Animal
4. Multiple Inheritance (via Interfaces)
Java does not support multiple inheritance through classes (to avoid ambiguity).
It is achieved via interfaces.
interface Flyable { void fly(); }
interface Swimmable { void swim(); }
class Duck implements Flyable, Swimmable {
public void fly() { [Link]("Duck flies"); }
public void swim() { [Link]("Duck swims"); }
}
5. Hybrid Inheritance
A combination of two or more types. Also achieved in Java via interfaces.
4. Method Overloading vs Method Overriding
Mnemonic: "Loading = Same name, different Luggage. Riding = Child replaces
Parent's ride."
Feature Overloading Overriding
Same method name, different Child class redefines parent's
Definition
parameters method
Where Within same class Between parent and child class
Parameters Must differ Must be same
Return type Can differ Must be same
Polymorphism
Compile-time (static) Runtime (dynamic)
type
OOP java 4
Feature Overloading Overriding
Inheritance
No Yes
needed?
Overloading Example:
class Calculator {
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; } // Same
name, different params
}
Overriding Example:
class Animal {
void sound() { [Link]("Some sound"); }
}
class Dog extends Animal {
void sound() { [Link]("Woof!"); } // Override
s parent method
}
5. Simple Java Program
// A simple Java program demonstrating a class with OOP basic
s
public class Student {
// Instance variables
String name;
int age;
double marks;
// Constructor
OOP java 5
Student(String name, int age, double marks) {
[Link] = name;
[Link] = age;
[Link] = marks;
}
// Method to display student info
void display() {
[Link]("Name : " + name);
[Link]("Age : " + age);
[Link]("Marks : " + marks);
}
// Main method — entry point
public static void main(String[] args) {
Student s1 = new Student("Alice", 20, 92.5);
[Link]();
}
}
Output:
Name : Alice
Age : 20
Marks : 92.5
6. Polymorphism — Types with Examples
Definition: Polymorphism means "many forms". The same method or object
behaves differently in different situations.
Mnemonic: "Poly = Many, Morph = Forms → One name, many behaviors"
Two Types:
1. Compile-Time Polymorphism (Static / Early Binding)
OOP java 6
Decided at compile time. Achieved through Method Overloading.
class Printer {
void print(int x) { [Link]("Integer: " +
x); }
void print(String s) { [Link]("String: " +
s); }
}
// Java decides WHICH print() to call at compile time based o
n argument type
2. Runtime Polymorphism (Dynamic / Late Binding)
Decided at runtime. Achieved through Method Overriding + Upcasting.
class Shape {
void draw() { [Link]("Drawing a shape"); }
}
class Circle extends Shape {
void draw() { [Link]("Drawing a Circle"); }
}
class Rectangle extends Shape {
void draw() { [Link]("Drawing a Rectangle");
}
}
public class Main {
public static void main(String[] args) {
Shape s;
s = new Circle(); [Link](); // Drawing a Circle
s = new Rectangle(); [Link](); // Drawing a Rectangle
// Which draw() runs is decided at RUNTIME
}
}
OOP java 7
7. Features of OOP
Mnemonic: "A PIE"
→ Abstraction, Polymorphism, Inheritance, Encapsulation
Feature Description
Wrapping data and methods together; hiding internal details using
Encapsulation
access modifiers
Inheritance A class acquiring properties of another class using extends
Polymorphism Same method behaving differently in different contexts
Abstraction Hiding complex implementation; showing only essential features
Class & Object Class is a blueprint; Object is a real-world instance
Message
Objects communicate by calling each other's methods
Passing
8. Drawbacks of Procedural Language & Need for OOP
Drawbacks of Procedural Language (POP):
Mnemonic: "GRIND" problems in POP:
Global data — insecure, any function can modify it
Reusability — poor, functions can't be easily reused across programs
Inflexible — hard to model real-world entities
No data hiding — no concept of encapsulation
Difficult maintenance — large programs become tangled ("spaghetti code")
Why OOP Was Needed:
Real-world problems are best modeled using objects (a Car, a Student, a Bank
Account)
Data security — private variables prevent unauthorized access
Code reuse — inheritance eliminates redundant code
Scalability — easy to extend programs by adding new classes
OOP java 8
Modularity — each class is independent and manageable
9. Primitive Data Types in Java
Mnemonic: "Big Students Find Long Distances Between Classrooms In School"
→ boolean, Short, Float, Long, Double, Byte, Char, Int
Data Type Size Default Value Range / Notes
byte 1 byte (8 bits) 0 -128 to 127
short 2 bytes 0 -32,768 to 32,767
int 4 bytes 0 ~-2 billion to 2 billion
long 8 bytes 0L Very large integers
float 4 bytes 0.0f Decimal (6-7 digit precision)
double 8 bytes 0.0d Decimal (15-16 digit precision)
char 2 bytes '\u0000' Single character (Unicode)
boolean ~1 bit false true or false only
// Examples
int age = 21;
double salary = 55000.50;
char grade = 'A';
boolean isPassed = true;
long population = 8000000000L;
float pi = 3.14f;
byte b = 100;
short s = 30000;
Quick Revision Mnemonics Summary:
OOP Features → A PIE
Access Modifiers → Public People Don't Prefer Privacy
Inheritance Types → Single Mum Has Many Hybrids
OOP java 9
Primitive Types → Big Students Find Long Distances Between Classrooms
In School
OOP java 10