📚 Chapters

☕ Java Programming

Unit 1 — Java Fundamentals & Inheritance

Chapter 1 — Java Fundamentals
☕ JAVA FUNDAMENTALS - MIND MAP
OOP Paradigms → Objects, Classes, Encapsulation, Inheritance, Polymorphism, Abstraction
Java Platform → JVM (bytecode executor), JDK (development), JRE (runtime)
Data Types → Primitive (8 types) & Reference (objects, arrays)
Operators → Arithmetic, Relational, Logical, Assignment, Unary
Control Flow → if-else, switch, for, while, do-while
OOP Building Blocks → Objects, Classes, Constructors, Methods, Access Specifiers, Static

1. Object-Oriented Programming Paradigms

📖 What is OOP?
A way to write programs using "objects" that hold data and actions. Code is organized around real-world things instead of just functions.

Why OOP? Before OOP, programs were written procedurally - code executed top-to-bottom sequentially. As programs grew larger, this became difficult to manage, debug, and maintain. OOP solves this by organizing code into logical units (objects) that mirror real-world entities.

💡 Real-life Example:
Think of a Car:
Object: Your specific red Toyota car parked outside
Attributes (Data): color="red", model="Toyota", speed=0, fuel=50L, engine="running"
Methods (Behavior): start(), stop(), accelerate(), brake(), refuel()
📌 Six Key OOP Concepts (Detailed Explanation):
1. Class:
A blueprint/template that defines what data and actions an object will have. No memory is used until an object is actually created from it.
// Class definition (blueprint) class Car { // Attributes/Properties String color; String model; int speed; // Behavior/Methods void start() { System.out.println("Car started"); } void accelerate() { speed += 10; System.out.println("Speed: " + speed); } }
2. Object:
An actual instance made from a class with real values in memory. Class = cookie cutter, Object = actual cookie. Many objects can be made from one class, each with different data.
// Creating objects (actual instances) Car myCar = new Car(); myCar.color = "Red"; myCar.model = "Toyota"; myCar.speed = 0; myCar.start(); // Output: Car started myCar.accelerate(); // Output: Speed: 10 Car yourCar = new Car(); yourCar.color = "Blue"; yourCar.model = "Honda"; // Different object, different values!
3. Encapsulation:
Wrapping data + methods in one class and hiding data using access modifiers (private/public). Outside code accesses data only through public methods — like a capsule hiding medicine inside.
💡 Real-life Analogy:
ATM machine - You can check balance and withdraw money (public methods), but you cannot directly access the vault or change your balance manually (private data).
class BankAccount { // Private data (hidden from outside) private double balance; private String accountNumber; // Public methods (controlled access) public void deposit(double amount) { if (amount > 0) { balance += amount; } } public double getBalance() { return balance; // Read-only access } } BankAccount acc = new BankAccount(); // acc.balance = 10000; // ERROR! Cannot access private acc.deposit(5000); // OK! Using public method
4. Inheritance:
Child class gets all properties and methods of a parent class and can add its own. Promotes reusability — write once, use many times. (IS-A rule: Dog IS-A Animal)
💡 Real-life Analogy:
You inherit features from your parents (eye color, height genes) but you're still a unique person with additional qualities.
// Parent class class Vehicle { String brand; int speed; void start() { System.out.println("Vehicle starting..."); } } // Child class inherits from Vehicle class Car extends Vehicle { int doors = 4; void drive() { System.out.println("Car driving..."); } } Car myCar = new Car(); myCar.brand = "Toyota"; // Inherited from Vehicle myCar.start(); // Inherited method myCar.drive(); // Own method
5. Polymorphism:
Same method name, different behavior depending on the object. Two types: overloading (same class, different params) and overriding (child redefines parent method).
💡 Real-life Analogy:
A person can be a student at college, son at home, employee at work - same person, different behaviors in different contexts.
class Animal { void sound() { System.out.println("Animal makes sound"); } } class Dog extends Animal { void sound() { System.out.println("Woof! Woof!"); } } class Cat extends Animal { void sound() { System.out.println("Meow! Meow!"); } } Animal a1 = new Dog(); Animal a2 = new Cat(); a1.sound(); // Output: Woof! Woof! a2.sound(); // Output: Meow! Meow! // Same method name, different behavior!
6. Abstraction:
Hide complex details, show only what the user needs. Focus on "what it does" not "how." Like using a car's accelerator — you don't need to know how the engine works.
💡 Real-life Analogy:
TV Remote - You press "Volume Up" button (simple interface) but don't know the complex electronic circuits inside that actually increase volume.
// Abstract class (cannot create object) abstract class Vehicle { // Abstract method (no implementation) abstract void start(); // Concrete method (has implementation) void stop() { System.out.println("Vehicle stopped"); } } class Car extends Vehicle { // Must provide implementation void start() { System.out.println("Car engine started"); } } // Vehicle v = new Vehicle(); // ERROR! Car c = new Car(); c.start(); // User doesn't see complex implementation

2. Features of Object-Oriented Programming

📖 Benefits of OOP: Makes code easier to write, debug, maintain, and scale compared to old procedural programming.
✓ Modularity: Code is divided into separate, independent modules (classes). Each class handles specific functionality. Easy to understand and maintain.

✓ Reusability: Write code once in a class, use it multiple times through objects and inheritance. Saves development time and effort.

✓ Maintainability: Changes in one class don't affect other classes (if properly encapsulated). Bug fixes and updates are easier.

✓ Security: Data hiding through encapsulation. Private variables cannot be accessed directly from outside. Controlled access through public methods.

✓ Scalability: Easy to add new features by creating new classes without modifying existing code. Supports large project growth.

✓ Code Organization: Logical structure that mirrors real-world entities. Easier for teams to collaborate.

✓ Flexibility: Polymorphism allows same interface for different data types. Makes code more flexible and extensible.

3. Java Platform — JVM, JDK, JRE

📖 "Write Once, Run Anywhere":
Java compiles to bytecode (not machine code). The JVM on any platform (Windows/Mac/Linux) runs this same bytecode — making Java platform-independent.
JDK — Java Development Kit:
The full package for developers. Contains everything needed to write, compile, and run Java programs.

Includes: JRE + Compiler (javac) + Debugger + Development tools
Use when: You are writing/developing Java code.
JRE — Java Runtime Environment:
The package needed to run Java programs. Does NOT include compiler.

Includes: JVM + Libraries (java.lang, java.util, etc.)
Use when: You only need to run a Java application (end user).
JVM — Java Virtual Machine:
The engine that actually executes bytecode line by line. It is platform-specific — Windows JVM, Mac JVM, Linux JVM — but they all understand the same bytecode.

Analogy: JVM = Translator that converts Java bytecode into OS-specific machine instructions.
ComponentFull FormContainsUsed By
JDKJava Development KitJRE + Compiler + ToolsDevelopers
JREJava Runtime EnvironmentJVM + LibrariesEnd users
JVMJava Virtual MachineBytecode executorBoth
💡 Memory Trick — Nested: JDK ⊃ JRE ⊃ JVM
JDK is the biggest (contains everything), JVM is the core engine inside.
💡 Java Compilation Flow:
Source (.java) → javac → Bytecode (.class) → JVM → Machine Code → Output

4. Data Types in Java

📖 Data Type: Specifies the type and size of data that a variable can store. Java is a statically typed language — you must declare the type before using a variable.

A. Primitive Data Types (8 types):

TypeSizeDefaultRange / Example
byte1 byte0-128 to 127
short2 bytes0-32,768 to 32,767
int4 bytes0-2B to 2B (most common)
long8 bytes0LVery large numbers, use L suffix
float4 bytes0.0fDecimal, use f suffix: 3.14f
double8 bytes0.0Precise decimal (most common)
char2 bytes'\u0000'Single character: 'A', '9'
boolean1 bitfalsetrue or false only

B. Reference Data Types:

Store memory addresses (references) to objects, not the value itself.
Examples: String, Arrays, Objects, Interfaces
// Primitive int age = 25; double price = 99.99; char grade = 'A'; boolean isActive = true; // Reference String name = "Ankush"; int[] numbers = {1, 2, 3, 4, 5}; Student s = new Student();
💡 Key Difference:
Primitive = stores actual value in stack.
Reference = stores address; object lives in heap.

5. Type Casting

📖 Type Casting: Converting a variable from one data type to another.
Widening (Implicit / Automatic):
Smaller type → Larger type. Java does it automatically. No data loss.
byte → short → int → long → float → double
Narrowing (Explicit / Manual):
Larger type → Smaller type. You must cast manually. Risk of data loss.
// Widening (automatic) int x = 100; double y = x; // int → double (automatic) System.out.println(y); // 100.0 // Narrowing (manual cast) double d = 99.99; int i = (int) d; // double → int (manual) System.out.println(i); // 99 (decimal part lost!)

6. Operators in Java

TypeOperatorsExample
Arithmetic+ - * / % ++ --a + b, a % 3, a++
Relational== != > < >= <=a == b, a > 5
Logical&& || !a>0 && b>0
Assignment= += -= *= /=a += 5 (same as a = a+5)
Bitwise& | ^ ~ << >>a & b, a << 2
Ternarycondition ? a : bmax = a>b ? a : b
int a = 10, b = 3; // Arithmetic System.out.println(a + b); // 13 System.out.println(a % b); // 1 (remainder) System.out.println(a++); // 10 (post-increment: use then add) System.out.println(++a); // 12 (pre-increment: add then use) // Ternary int max = (a > b) ? a : b; System.out.println("Max: " + max); // Max: 12 // Logical boolean result = (a > 0) && (b > 0); System.out.println(result); // true

7. Arrays

📖 Array: A collection of elements of the same data type stored in contiguous memory locations. Index starts at 0.
// 1D Array int[] marks = new int[5]; // Declare with size int[] scores = {85, 90, 78, 92, 88}; // Declare with values System.out.println(scores[0]); // 85 (first element) System.out.println(scores.length); // 5 // Traverse with loop for (int i = 0; i < scores.length; i++) { System.out.println(scores[i]); } // Enhanced for loop for (int score : scores) { System.out.println(score); } // 2D Array int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; System.out.println(matrix[1][2]); // 6 (row 1, col 2)
💡 Key Points:
• Arrays are fixed size — cannot grow or shrink
• Index starts at 0, last index = length - 1
• Accessing out-of-bounds index throws ArrayIndexOutOfBoundsException

8. String Class

📖 String: A sequence of characters. In Java, String is a class (reference type), not a primitive. Strings are immutable — once created, their value cannot be changed.
String name = "Ankush"; String greeting = new String("Hello"); // Common String methods System.out.println(name.length()); // 6 System.out.println(name.toUpperCase()); // ANKUSH System.out.println(name.toLowerCase()); // ankush System.out.println(name.charAt(0)); // A System.out.println(name.indexOf('k')); // 2 System.out.println(name.substring(0, 3)); // Ank System.out.println(name.contains("ush")); // true System.out.println(name.replace("ush", "")); // Ank System.out.println(name.equals("Ankush")); // true (use this!) System.out.println(name.trim()); // removes spaces System.out.println("Hi " + name); // Hi Ankush (concat)
⚠️ Important: Always use .equals() to compare Strings, NOT ==.
== compares references (memory address).
.equals() compares actual content/value.
💡 StringBuilder vs String:
String is immutable — every modification creates a new object (wasteful).
StringBuilder is mutable — modify in place, much faster for repeated changes.
// StringBuilder (mutable, efficient) StringBuilder sb = new StringBuilder("Hello"); sb.append(" World"); // Modifies same object sb.insert(5, ","); sb.reverse(); System.out.println(sb.toString()); // dlroW ,olleH

9. Control Statements

1. if-else Statement:
if (condition) { // code if true } else { // code if false } // Example int marks = 85; if (marks >= 75) { System.out.println("Grade A"); } else { System.out.println("Grade B"); }
2. switch Statement:
switch (variable) { case value1: // code break; case value2: // code break; default: // code } // Example int day = 3; switch (day) { case 1: System.out.println("Monday"); break; case 2: System.out.println("Tuesday"); break; case 3: System.out.println("Wednesday"); break; default: System.out.println("Invalid"); }
3. for Loop:
for (initialization; condition; update) { // code } // Example for (int i = 1; i <= 5; i++) { System.out.println(i); }
4. while Loop:
while (condition) { // code } // Example int i = 1; while (i <= 5) { System.out.println(i); i++; }
5. do-while Loop:
do { // code } while (condition); // Example int i = 1; do { System.out.println(i); i++; } while (i <= 5);

10. Programming Structures in Java

1. Sequential Structure: Code executes line by line from top to bottom.
2. Selection Structure: Code executes based on conditions (if-else, switch).
3. Iteration Structure: Code repeats multiple times (for, while, do-while).

11. Objects and Classes in Java

📖 Class: Blueprint that defines structure and behavior.
📖 Object: Instance of a class with actual values.
// Class definition class Student { String name; int rollNo; void display() { System.out.println("Name: " + name); System.out.println("Roll No: " + rollNo); } } // Creating objects Student s1 = new Student(); s1.name = "Raj"; s1.rollNo = 101; s1.display();
💡 Class vs Object:
Class = Cookie cutter (template)
Object = Actual cookies (instances)

12. Constructors

📖 Constructor: Special method that initializes objects. Called automatically when object is created using 'new' keyword.
Rules:
• Name = Class name
• No return type
• Called automatically
• Can be overloaded
class Student { String name; int rollNo; // Default constructor Student() { name = "Unknown"; rollNo = 0; } // Parameterized constructor Student(String n, int r) { name = n; rollNo = r; } } // Usage Student s1 = new Student(); Student s2 = new Student("Raj", 101);

13. Methods

📖 Method: Block of code that performs specific task. Also called functions.
returnType methodName(parameters) { // code return value; } // Examples class Calculator { int add(int a, int b) { return a + b; } void greet() { System.out.println("Hello!"); } } Calculator calc = new Calculator(); int sum = calc.add(10, 20); calc.greet();

14. Access Specifiers

📖 Access Specifiers: Control visibility and accessibility of class members.
Modifier Class Package Subclass World
public
protected
default
private
class BankAccount { public String accountHolder; private double balance; protected String accountType; int accountNumber; // default public void deposit(double amount) { balance += amount; } }

15. Static Members

📖 Static: Belongs to class, not to individual objects. Shared by all instances.
class Student { String name; // Instance variable static String school; // Static variable (shared) static int count = 0; Student(String n) { name = n; count++; } static void displaySchool() { System.out.println("School: " + school); } } // Usage Student.school = "ABC School"; Student s1 = new Student("Raj"); Student s2 = new Student("Priya"); System.out.println(Student.count); // 2 Student.displaySchool();
💡 Key Points:
• Static members shared by all objects
• Access using ClassName.member
• Static method can access only static members
• Cannot use 'this' in static context

16. Functions

📖 Functions: In Java, functions are called methods. They are blocks of reusable code.
// Function types class MathUtils { // No return, no parameters void greet() { System.out.println("Hello!"); } // Return value, with parameters int add(int a, int b) { return a + b; } // No return, with parameters void printSum(int a, int b) { System.out.println("Sum: " + (a + b)); } }
🔗 Chapter 2 — Classes, Inheritance
🔗 CLASSES & INHERITANCE - MIND MAP
Overloading → Same name, different parameters (compile-time polymorphism)
Overriding → Child redefines parent method (runtime polymorphism)
Objects → Can be passed as parameters, returned from methods
Classes → Static (class-level), Inner (nested), Wrapper (primitive to object)
Inheritance → Single, Multilevel, Hierarchical, Multiple (interface only)
Keywords → this (current object), super (parent), abstract, final, interface

1. Overloading and Overriding Methods

Method Overloading (Compile-time Polymorphism):
📖 Overloading:
Multiple methods with the same name but different parameters in the same class. Compiler picks the right one at compile time. No need for names like addTwoInt(), addThreeInt().
💡 Real-life Analogy:
A calculator can add() two numbers, three numbers, or decimal numbers - same operation name, different inputs. You don't need separate methods like addTwoNumbers(), addThreeNumbers(), addDecimals().
class Calculator { // Overloaded methods - same name, different parameters // Method 1: Two integers int add(int a, int b) { return a + b; } // Method 2: Three integers int add(int a, int b, int c) { return a + b + c; } // Method 3: Two doubles double add(double a, double b) { return a + b; } } Calculator calc = new Calculator(); System.out.println(calc.add(5, 10)); // Calls method 1 → 15 System.out.println(calc.add(5, 10, 15)); // Calls method 2 → 30 System.out.println(calc.add(5.5, 10.5)); // Calls method 3 → 16.0
💡 Overloading Rules:
• Must have different parameter lists (number, type, or order)
• Return type alone is NOT enough to overload
• Can have different access modifiers
• Can throw different exceptions
Method Overriding (Runtime Polymorphism):
📖 Overriding:
Child class redefines a parent method with the exact same name and params. Which version runs is decided at runtime. Lets child classes customize inherited behavior.
💡 Real-life Analogy:
Parent says "go to school by bus" (general instruction), but child overrides with "I'll go by bicycle" - same action (going to school), different implementation. The child customizes the parent's method.
class Animal { // Parent class method void sound() { System.out.println("Animal makes a sound"); } void eat() { System.out.println("Animal eats"); } } class Dog extends Animal { // Overriding parent's sound() method @Override // Annotation (optional but recommended) void sound() { System.out.println("Dog barks: Woof! Woof!"); } // eat() is inherited as-is } class Cat extends Animal { // Overriding parent's sound() method differently @Override void sound() { System.out.println("Cat meows: Meow! Meow!"); } } Animal a = new Dog(); a.sound(); // Output: Dog barks: Woof! Woof! (runtime decision) a.eat(); // Output: Animal eats (inherited)
💡 Overriding Rules:
• Method signature must be exactly same as parent
• Access modifier must be same or less restrictive
• Return type must be same or covariant (subtype)
• Cannot override final, static, or private methods
• @Override annotation helps catch mistakes
Feature Overloading Overriding
Location Same class Parent-child classes
Parameters Must be different Must be exactly same
Binding Compile-time (early) Runtime (late)
Return Type Can be different Same or covariant
Purpose Increase readability Customize behavior

2. Objects as Parameters

📖 Objects as Parameters:
Objects can be passed to methods just like int or double. Java passes the reference (address), not a copy — so the method works on the actual object.
💡 Real-life Analogy:
Passing your ID card to a security guard - you're giving them the object (ID) to verify details. They can read information from your ID without creating a copy.
class Student { String name; int marks; // Constructor Student(String name, int marks) { this.name = name; this.marks = marks; } // Method accepting object as parameter boolean hasBetterMarks(Student other) { return this.marks > other.marks; } // Method to compare two students static void compareStudents(Student s1, Student s2) { if (s1.marks > s2.marks) { System.out.println(s1.name + " has better marks"); } else { System.out.println(s2.name + " has better marks"); } } } // Creating objects Student raj = new Student("Raj", 85); Student priya = new Student("Priya", 92); // Passing objects as parameters if (raj.hasBetterMarks(priya)) { System.out.println("Raj is better"); } else { System.out.println("Priya is better"); } Student.compareStudents(raj, priya); // Output: Priya has better marks

3. Returning Objects

📖 Returning Objects:
A method can return an object (its reference). Useful in factory methods that create and give back new objects.
💡 Real-life Analogy:
A photocopying machine returns a copy (new object) of your document - the method creates and returns a new object to you.
class Student { String name; int rollNo; Student(String name, int rollNo) { this.name = name; this.rollNo = rollNo; } // Method returning object (creates a copy) Student createCopy() { Student copy = new Student(this.name, this.rollNo); return copy; // Returns reference to new object } // Static method returning object (factory pattern) static Student createDefault() { return new Student("Unknown", 0); } void display() { System.out.println("Name: " + name + ", Roll: " + rollNo); } } Student original = new Student("Raj", 101); Student copy = original.createCopy(); // Getting returned object copy.name = "Raj Copy"; // Modify copy original.display(); // Output: Name: Raj, Roll: 101 copy.display(); // Output: Name: Raj Copy, Roll: 101 Student defaultStudent = Student.createDefault(); defaultStudent.display(); // Output: Name: Unknown, Roll: 0

4. Static, Inner and Wrapper Classes

Static Nested Class:
📖 Static Nested Class:
A class inside another class marked with static. Linked to the outer class (not its objects). Can only access static members of the outer class.
class Outer { static int x = 10; int y = 20; static class StaticNested { void display() { System.out.println("x = " + x); // Can access static // System.out.println(y); // ERROR! Cannot access non-static } } } // Creating object Outer.StaticNested obj = new Outer.StaticNested(); obj.display(); // Output: x = 10
Inner Class (Non-static):
📖 Inner Class:
A non-static class inside another class. Linked to an instance of the outer class and can access all its members (static + non-static).
class Outer { int x = 20; class Inner { void display() { System.out.println("x = " + x); // Can access non-static } } } // Creating object (need outer object first) Outer outer = new Outer(); Outer.Inner inner = outer.new Inner(); inner.display(); // Output: x = 20
Wrapper Classes:
📖 Wrapper Classes:
Convert primitives into objects (int → Integer, char → Character). Needed because collections only store objects. Also provide utility methods like parseInt().
Primitive Wrapper Class
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble
charCharacter
booleanBoolean
// Auto-boxing (primitive to object) int num = 10; Integer obj = num; // Automatic conversion // Auto-unboxing (object to primitive) Integer obj2 = 20; int num2 = obj2; // Automatic conversion // Useful methods String str = "123"; int n = Integer.parseInt(str); // String to int System.out.println(n + 5); // Output: 128 // Collections need objects ArrayList list = new ArrayList<>(); list.add(10); // Auto-boxing happens

5. Inheritance and Types of Inheritance

📖 Inheritance:
Child class gets all properties and methods of parent using extends, and can add its own. Promotes reusability and IS-A relationship (Dog IS-A Animal).
💡 Real-life Example:
Dog IS-A Animal (inherits eating, breathing) but also has its own features (barking). Car IS-A Vehicle (inherits starting, stopping) but has its own features (4 wheels).
1. Single Inheritance:
One child class inherits from one parent class. A → B
class Vehicle { void start() { System.out.println("Vehicle started"); } } class Car extends Vehicle { void drive() { System.out.println("Car driving"); } } Car c = new Car(); c.start(); // Inherited c.drive(); // Own method
2. Multilevel Inheritance:
Chain of inheritance. A → B → C (B inherits from A, C inherits from B)
class Vehicle { void start() { } } class Car extends Vehicle { void drive() { } } class SportsCar extends Car { void turbo() { } } SportsCar sc = new SportsCar(); sc.start(); // From Vehicle sc.drive(); // From Car sc.turbo(); // Own method
3. Hierarchical Inheritance:
One parent class, multiple child classes. A → B, A → C
class Animal { void eat() { System.out.println("Eating..."); } } class Dog extends Animal { void bark() { System.out.println("Barking..."); } } class Cat extends Animal { void meow() { System.out.println("Meowing..."); } }
4. Multiple Inheritance (Using Interface):
One class inherits from multiple parents. NOT supported with classes (diamond problem) but supported through interfaces.
interface Flyable { void fly(); } interface Swimmable { void swim(); } class Duck implements Flyable, Swimmable { public void fly() { System.out.println("Duck flying"); } public void swim() { System.out.println("Duck swimming"); } }

6. this and Super Keyword

this Keyword:
📖 this keyword:
Refers to the current object. Used to separate instance variables from same-named parameters, and to call another constructor within the same class.
class Student { String name; int age; Student(String name, int age) { this.name = name; // this.name = instance variable this.age = age; // name, age = parameters } Student() { this("Unknown", 0); // Call another constructor } void display() { System.out.println(this.name + ", " + this.age); } }
super Keyword:
📖 super keyword:
Refers to the immediate parent class. Used to call parent's constructor, access parent's variable, or call parent's method when child has same-named members.
class Vehicle { int speed = 50; void display() { System.out.println("Vehicle display"); } } class Car extends Vehicle { int speed = 100; Car() { super(); // Call parent constructor } void show() { System.out.println("Car speed: " + speed); // 100 System.out.println("Vehicle speed: " + super.speed); // 50 super.display(); // Parent method } }

7. Abstract Classes

📖 Abstract Class:
Declared with abstract — cannot create its objects directly. Has abstract methods (no body) + normal methods. Child classes must implement all abstract methods.
💡 Real-life Example:
"Shape" is abstract (you can't draw a generic shape), but Circle and Rectangle are concrete implementations.
abstract class Shape { String color; // Abstract method (no body) abstract void draw(); // Concrete method (has body) void setColor(String color) { this.color = color; System.out.println("Color set to " + color); } } class Circle extends Shape { void draw() { // Must implement System.out.println("Drawing circle"); } } // Shape s = new Shape(); // ERROR! Cannot instantiate Circle c = new Circle(); c.setColor("Red"); c.draw();

8. final Variables and Methods

📖 Definition:
'final' is a keyword used to apply restrictions. Can be applied to variables, methods, and classes.

final variable: Value cannot be changed (constant)
final method: Cannot be overridden
final class: Cannot be inherited
// final variable final int MAX_SPEED = 120; // MAX_SPEED = 150; // ERROR! Cannot change // final method class Parent { final void show() { System.out.println("Final method"); } } class Child extends Parent { // void show() { } // ERROR! Cannot override } // final class final class Vehicle { } // class Car extends Vehicle { } // ERROR! Cannot inherit

9. Interface

📖 Interface:
A contract a class must follow. Contains abstract methods (no body). Class uses implements and must define all methods. Achieves 100% abstraction + supports multiple inheritance.
💡 Real-life Example:
TV Remote interface - defines buttons (methods) that any TV must implement, but each TV brand implements them differently.
interface Animal { void eat(); // public abstract (by default) void sleep(); } class Dog implements Animal { public void eat() { System.out.println("Dog eats"); } public void sleep() { System.out.println("Dog sleeps"); } } Dog d = new Dog(); d.eat(); d.sleep();

10. Member Access

📖 Member Access: Controls who can access class variables and methods. Java has 4 levels: public (anywhere), protected (package + subclasses), default (package only), private (class only).
Access Level Summary:
public: Accessible everywhere
protected: Accessible in same package + subclasses
default: Accessible only in same package
private: Accessible only within same class
class Example { public int a = 10; // Accessible everywhere protected int b = 20; // Package + subclasses int c = 30; // Default: Package only private int d = 40; // Class only public void display() { System.out.println(d); // Can access private here } }
Ready for Exam? Sab padh liya? Ab Quick Revision karo — formulas, key points aur common mistakes ek jagah! Quick Revision Karo →
Quick Revision — Last Minute Exam Prep!
📌 How to Use: Read this 5-10 minutes before exam. Contains all important points in condensed form. Focus on tables, comparisons, and PYQ topics!

☕ new Keyword (2M)

Purpose of 'new':
1. Allocates memory in heap
2. Creates object instance
3. Calls constructor
4. Returns reference

Syntax: ClassName obj = new ClassName();

📦 Class vs Object (2M)

Class | Object ------------------- | ------------------- Blueprint/Template | Instance Logical entity | Physical entity No memory | Memory allocated Defined once | Can create many class Student { } | Student s = new Student(); "Car" concept | "My red Toyota"
Exam Trick: Class = Blueprint, Object = Instance (B vs I)

💾 Static vs Non-Static (2M)

Static Members | Non-Static Members ---------------------- | ---------------------- Belong to class | Belong to object Method Area | Heap Memory One copy (shared) | Separate per object When class loads | When object created ClassName.member | objectName.member static int count; | int age;
Rules:
• Static CAN access static only
• Static CANNOT access non-static directly
• Non-static CAN access both
• No 'this' or 'super' in static
Remember S.H.A.R.E: Static = Held in method Area, Reused by Everyone

🔄 Overloading vs Overriding (IMP!)

Overloading | Overriding --------------------- | --------------------- Same class | Parent-child classes Different parameters | Same signature Compile-time | Runtime Can change return | Same return type No inheritance needed | Inheritance required add(int,int) | Parent: show() add(double,double) | Child: show()
Trick:
Overloading = Same NAME, Different PARAMS
Overriding = Different CLASS, Same SIGNATURE

🔗 Inheritance Types

Single: A → B
Multilevel: A → B → C
Hierarchical: A → B, A → C
Multiple: NOT with classes (use Interface)
Hybrid: NOT with classes
Syntax: class Child extends Parent { }

👆 this vs super

this (current object) | super (parent class) --------------------- | ---------------------- this.variable | super.variable this() | super() this (pass object) | super.method() Same class reference | Parent class reference
Rules:
• super() must be FIRST in constructor
• Cannot use both super() and this() together

🎭 Abstract vs Interface (IMP!)

Abstract Class | Interface --------------------- | --------------------- abstract keyword | interface keyword 0-100% abstraction | 100% abstraction Can have constructors | No constructors extends (single) | implements (multiple) Any access modifier | Only public Can have variables | Only constants (final) abstract + concrete | Only abstract (before Java 8)
abstract class Animal { abstract void sound(); void sleep() { } } interface Flyable { void fly(); // public abstract }

🔐 final Keyword

final variable: Constant (cannot change)
final method: Cannot override
final class: Cannot inherit
final int MAX = 100; final void show() { } final class String { }

⚠️ Checked vs Unchecked (PYQ!)

Checked | Unchecked --------------------- | --------------------- Compile-time check | Runtime only Must handle (forced) | Optional to handle Exception class | RuntimeException class External factors | Programming errors IOException | NullPointerException SQLException | ArrayIndexOutOfBounds FileNotFoundException | ArithmeticException
C.O.R.T Trick:
Checked: Compile-time, Outside control
Unchecked: Runtime, Typically programmer fault

🛡️ Why Catch Exceptions? (2M)

Prevents program crash
Maintains normal flow
User-friendly error messages
Resource cleanup (finally)
Debugging information
Professional code quality
try { // Risky code } catch (Exception e) { // Handle error } finally { // Always executes (cleanup) }

🎯 throw vs throws (5M PYQ!)

throw | throws --------------------- | --------------------- Inside method body | Method signature Throw exception | Declare exception Exception object | Exception class One at a time | Multiple allowed throw new Exception() | void m() throws Exception Creates & throws | Only declares
Example: void withdraw(int amt) throws InsufficientFundsException { if (amt > balance) { throw new InsufficientFundsException("Low balance"); } }
Memory Trick:
throw = action (doing it)
throws = declaration (warning about it)

⚠️ Common Exam Mistakes

❌ Overriding static/final/private methods
❌ Using 'this' or 'super' in static
❌ Multiple inheritance with classes
❌ Creating object of abstract class/interface
❌ Empty catch blocks (bad practice)
❌ Not handling checked exceptions
❌ Forgetting @Override annotation
❌ Confusing throw vs throws

💻 Must Practice Programs

1. Method overloading example (Calculator)
2. Method overriding (Animal → Dog/Cat)
3. Single, multilevel, hierarchical inheritance
4. Abstract class implementation
5. Interface with multiple implementation
6. this and super keyword usage
7. Static vs non-static demonstration
8. Exception handling (try-catch-finally)
9. throw vs throws example
10. Custom exception creation

✅ Pre-Exam Checklist

☑ new keyword purpose (3 points)
☑ Class vs Object table
☑ Static vs Non-static table
☑ Overloading vs Overriding table
☑ Inheritance types (3 supported, 2 not)
☑ this vs super usage
☑ Abstract vs Interface table
☑ final keyword (3 uses)
☑ Checked vs Unchecked table
☑ Why catch exceptions (6 reasons)
☑ throw vs throws table
☑ Common mistakes list

🎯 Exam Strategy

2 Mark Questions:
• 2-3 sentences or 3-4 points
• Time: 3-4 minutes max
• Add example if time permits

5 Mark Questions:
• Definition + Explanation + Code + Diagram
• Time: 7-8 minutes
• Draw comparison tables when asked "differentiate"

Code Questions:
• Write proper syntax
• Add comments
• Show output if asked
• Use meaningful variable names
🌟 All the Best!
Java is practice-based! Don't just read—write code. The more you code, the better you understand. Remember all comparisons (tables), practice all programs, and you're ready! 💪☕
📄 Previous Year Questions

Section A — 2 Marks PYQs

2M PYQ
Q1. State the purpose of the 'new' keyword in Java.
Answer:

The new keyword in Java is used to create objects (instances) of a class. Its main purposes are:

1. Memory Allocation: Allocates memory dynamically in the heap for the object.

2. Object Creation: Creates an instance of the class with its own copy of instance variables.

3. Constructor Invocation: Automatically calls the constructor to initialize the object.

4. Returns Reference: Returns the memory reference (address) of the newly created object.

Example:
Student s = new Student(); // new allocates memory, calls constructor, returns reference
Without new, you cannot create objects in Java (except for String and primitives which have special handling).
2M PYQ
Q2. Compare a class and an object with differences and examples.
Answer:

Class: A blueprint or template that defines the structure and behavior of objects.
Object: An instance of a class with actual values.

Key Differences:

1. Nature:
• Class: Logical entity (concept)
• Object: Physical entity (real)

2. Memory:
• Class: No memory allocated
• Object: Memory allocated when created

3. Creation:
• Class: Defined using 'class' keyword
• Object: Created using 'new' keyword

4. Count:
• Class: Defined only once
• Object: Can create multiple objects

Example:
// Class - Blueprint class Student { String name; int rollNo; } // Objects - Instances Student s1 = new Student(); // Object 1 s1.name = "Raj"; Student s2 = new Student(); // Object 2 s2.name = "Priya";
Real-life: Class = "Car" (general concept), Object = "My red Toyota" (specific car)
2M PYQ
Q3. Explain memory management for static vs. non-static members in Java.
Answer:

Static Members (Class Level):
• Stored in Method Area (class memory)
• Allocated when class loads (only once)
• Shared by all objects
• Single copy exists for entire class
• Accessed using ClassName.member

Non-Static Members (Object Level):
• Stored in Heap Memory (object memory)
• Allocated when object is created (each time)
• Separate copy for each object
• Independent values per object
• Accessed using objectName.member

Example:
class Student { static String school; // Static - shared by all String name; // Non-static - separate per object } Student.school = "ABC"; // One copy for all Student s1 = new Student(); s1.name = "Raj"; // s1's own copy Student s2 = new Student(); s2.name = "Priya"; // s2's own copy
Key Point: Static members save memory as only one copy exists, while non-static members allow each object to have unique values.
2M PYQ
Q4. Define checked and unchecked exceptions.
Answer:

Checked Exceptions:
Exceptions that are checked at COMPILE TIME. The compiler forces you to handle them using try-catch or throws. If not handled, the program won't compile.

• Checked at: Compile time
• Handling: Mandatory (must handle)
• Extends: Exception class
• Cause: External factors (file, network, database)
• Examples: IOException, FileNotFoundException, SQLException

Unchecked Exceptions:
Exceptions that occur at RUNTIME due to programming errors. Also called Runtime Exceptions. Compiler doesn't force handling.

• Checked at: Runtime
• Handling: Optional
• Extends: RuntimeException class
• Cause: Programming errors
• Examples: NullPointerException, ArrayIndexOutOfBoundsException, ArithmeticException

Simple Rule:
Checked = Compiler checks, must handle
Unchecked = Runtime only, optional handling
2M PYQ
Q5. Explain why catching exceptions is recommended in Java.
Answer:

Catching exceptions is recommended because:

1. Prevents Program Crash: Program doesn't terminate abruptly when error occurs.

2. Maintains Normal Flow: Remaining code continues to execute after handling the exception.

3. User-Friendly Messages: Provides meaningful error messages instead of technical stack traces.

4. Resource Cleanup: Using finally block ensures resources (files, connections) are properly closed even if error occurs.

5. Debugging Information: Can log exception details for troubleshooting.

6. Professional Code: Production-ready applications must handle errors gracefully.

Example:
// Without handling - CRASH int result = 10 / 0; // ArithmeticException, program stops // With handling - CONTINUES try { int result = 10 / 0; } catch (ArithmeticException e) { System.out.println("Cannot divide by zero"); } System.out.println("Program continues...");

Section B — 5 Marks PYQs

5M PYQ
Q1. Demonstrate how Java supports multiple inheritance using interfaces by implementing a Java program that involves two interfaces and a class. Apply the concept to show why interfaces are preferred over multiple class inheritance in Java.
Answer:

Multiple Inheritance Problem:
Java does NOT support multiple inheritance with classes to avoid the "Diamond Problem" (ambiguity when both parent classes have the same method). However, Java achieves multiple inheritance through INTERFACES.

Why Interfaces are Preferred:
1. No ambiguity - all methods are abstract, must be implemented by class
2. Avoids diamond problem
3. A class can implement multiple interfaces
4. Provides flexibility and multiple behavior support

Program Demonstration:
// Interface 1 interface Flyable { void fly(); int MAX_ALTITUDE = 10000; // constant } // Interface 2 interface Swimmable { void swim(); int MAX_DEPTH = 500; // constant } // Class implementing both interfaces (Multiple Inheritance) class Duck implements Flyable, Swimmable { private String name; Duck(String name) { this.name = name; } // Must implement fly() from Flyable public void fly() { System.out.println(name + " is flying"); System.out.println("Max altitude: " + MAX_ALTITUDE); } // Must implement swim() from Swimmable public void swim() { System.out.println(name + " is swimming"); System.out.println("Max depth: " + MAX_DEPTH); } void displayInfo() { System.out.println("I am a " + name); } } // Main class class Main { public static void main(String[] args) { Duck duck = new Duck("Donald Duck"); duck.displayInfo(); // I am a Donald Duck duck.fly(); // Donald Duck is flying duck.swim(); // Donald Duck is swimming // Duck has behavior from both interfaces System.out.println("Duck supports multiple inheritance!"); } } Output: I am a Donald Duck Donald Duck is flying Max altitude: 10000 Donald Duck is swimming Max depth: 500 Duck supports multiple inheritance!
Explanation:
• Duck class implements both Flyable and Swimmable interfaces
• This is multiple inheritance - one class inheriting from multiple sources
• Duck must implement all methods from both interfaces
• No ambiguity because all interface methods are abstract
• Class decides the implementation, avoiding conflicts

Why Not Multiple Class Inheritance:
// This is NOT allowed in Java class A { void show() { System.out.println("A"); } } class B { void show() { System.out.println("B"); } } // ERROR! Cannot extend multiple classes // class C extends A, B { } // Which show() to inherit? Ambiguity!
Conclusion:
Interfaces solve multiple inheritance by requiring the implementing class to provide all method implementations, eliminating ambiguity while providing flexibility to inherit behavior from multiple sources.
5M PYQ
Q2. Differentiate between 'throw' and 'throws' with example code.
Answer:

Difference Between throw and throws:

Aspect throw throws
Purpose Throw exception explicitly Declare possible exceptions
Location Inside method body Method signature
Followed By Exception object (instance) Exception class (type)
Count One exception at a time Multiple exceptions allowed
Syntax throw new Exception(); void method() throws Exception
Action Creates and throws Only declares possibility

Example Code with Both:
import java.io.*; // Custom Exception class InsufficientBalanceException extends Exception { InsufficientBalanceException(String message) { super(message); } } class BankAccount { private int balance; BankAccount(int initialBalance) { balance = initialBalance; } // 'throws' declares method may throw exception void withdraw(int amount) throws InsufficientBalanceException { System.out.println("Attempting to withdraw: " + amount); if (amount > balance) { // 'throw' actually throws the exception throw new InsufficientBalanceException( "Insufficient balance. Available: " + balance ); } balance -= amount; System.out.println("Withdrawal successful"); System.out.println("Remaining balance: " + balance); } // Method with multiple exception declarations void processTransaction() throws IOException, InsufficientBalanceException { // Can throw multiple types throw new IOException("Network error"); } int getBalance() { return balance; } } class Main { public static void main(String[] args) { BankAccount account = new BankAccount(1000); System.out.println("Initial balance: " + account.getBalance()); // Test 1: Valid withdrawal try { account.withdraw(500); } catch (InsufficientBalanceException e) { System.out.println("Error: " + e.getMessage()); } // Test 2: Invalid withdrawal (exceeds balance) try { account.withdraw(800); // Only 500 left } catch (InsufficientBalanceException e) { System.out.println("Error: " + e.getMessage()); } System.out.println("Final balance: " + account.getBalance()); } } Output: Initial balance: 1000 Attempting to withdraw: 500 Withdrawal successful Remaining balance: 500 Attempting to withdraw: 800 Error: Insufficient balance. Available: 500 Final balance: 500
Explanation:

1. throws keyword:
• Used in method signature: void withdraw(...) throws InsufficientBalanceException
• Declares that this method MAY throw an exception
• Informs the caller to handle the exception
• Can declare multiple exceptions: throws IOException, SQLException

2. throw keyword:
• Used inside method body: throw new InsufficientBalanceException(...)
• Actually creates and throws the exception object
• Execution stops at this point and jumps to catch block
• Can only throw one exception at a time

Real-world Analogy:
throws: Sign on door saying "Beware: Area may have obstacles"
throw: Actually placing an obstacle on the path

Key Point: throws is a warning/declaration, throw is the actual action of throwing an exception.