Java Revision Notes
Exception Handling
Definition: Exception handling in Java is a way to handle runtime errors so the normal flow of the
program is not disrupted.
Keywords: try, catch, finally, throw, throws
Example:
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero.");
} finally {
System.out.println("This block always runs.");
File Handling
Definition: File handling allows Java programs to read from and write to files stored on the disk.
Example:
FileWriter writer = new FileWriter("example.txt");
writer.write("Hello, Java File Handling!");
writer.close();
BufferedReader reader = new BufferedReader(new FileReader("example.txt"));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
reader.close();
Threads
Definition: A thread is a lightweight process. Java allows multiple threads to run concurrently.
Example using Thread class:
class MyThread extends Thread {
public void run() {
System.out.println("Thread is running...");
Example using Runnable interface:
class MyRunnable implements Runnable {
public void run() {
System.out.println("Runnable thread is running...");
Multithreading
Definition: Multithreading is the process of executing multiple threads simultaneously.
Example:
class MyThread extends Thread {
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println(Thread.currentThread().getName() + " - Count: " + i);
Encapsulation
Definition: Wrapping data and methods into a single unit using private variables and public methods.
Example:
class Student {
private String name;
public void setName(String name) {
this.name = name;
public String getName() {
return name;
Inheritance
Definition: One class inherits properties and methods from another using 'extends'.
Example:
class Animal {
void sound() {
System.out.println("Animal makes a sound");
class Dog extends Animal {
void bark() {
System.out.println("Dog barks");
Polymorphism
Definition: One method behaves differently based on the object or data.
Method Overloading:
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
Method Overriding:
class Animal {
void sound() { System.out.println("Animal sound"); }
class Cat extends Animal {
void sound() { System.out.println("Meow"); }
Abstraction
Definition: Hiding complex implementation details and showing only essential features.
Using Abstract Class:
abstract class Shape {
abstract void draw();
class Circle extends Shape {
void draw() {
System.out.println("Drawing Circle");
Using Interface:
interface Animal {
void makeSound();
class Cow implements Animal {
public void makeSound() {
System.out.println("Moo");