0% found this document useful (0 votes)
23 views9 pages

Unit3 Oops

The document covers key concepts in Java, including nested classes, exception handling, concurrent programming, thread management, and I/O streams. It explains the types of nested classes, how to handle exceptions, the benefits of concurrent programming, and methods for creating threads. Additionally, it provides examples and a summary sheet for quick revision of these topics.

Uploaded by

jaspinder1805
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views9 pages

Unit3 Oops

The document covers key concepts in Java, including nested classes, exception handling, concurrent programming, thread management, and I/O streams. It explains the types of nested classes, how to handle exceptions, the benefits of concurrent programming, and methods for creating threads. Additionally, it provides examples and a summary sheet for quick revision of these topics.

Uploaded by

jaspinder1805
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

UNIT-3

✅ 1. Nested Classes
🔹 What are Nested Classes?
Nested classes are classes defined within another class.
🔹 Types of Nested Classes in Java:
pgsql
CopyEdit
OuterClass
├── Static Nested Class
└── Inner Class
├── Member Inner Class
├── Local Inner Class
└── Anonymous Inner Class
🔹 Diagram:
pgsql
CopyEdit
+--------------------+
| OuterClass |
| |
| +----------------+ |
| | StaticNested | | <--- like a static member
| +----------------+ |
| |
| +----------------+ |
| | MemberInner | | <--- accesses outer members
| +----------------+ |
| |
| method() { |
| class Local {} | <--- defined in method
|} |
+--------------------+
🔹 Example:
java
CopyEdit
class Outer {
int x = 10;

class Inner {
void show() {
System.out.println("x = " + x); // access outer class
variable
}
}
static class StaticInner {
void display() {
System.out.println("Inside static inner class");
}
}
}

✅ 2. Exception Handling
🔹 Key Concepts:
 Exception: Runtime error (e.g., divide by zero, file not
found).
 Try-Catch-Finally: Used to handle exceptions.
 Throw: Used to explicitly throw an exception.
 Throws: Declares an exception might occur.
🔹 Flowchart:
vbnet
CopyEdit
START
|
try block
|
[Exception?]
| \
Yes No
| |
catch block Continue
|
finally block
|
END
🔹 Example:
java
CopyEdit
try {
int a = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero!");
} finally {
System.out.println("This always runs.");
}

✅ 3. Exception Handlers
🔹 Common Handlers:
 ArithmeticException
 ArrayIndexOutOfBoundsException
 NullPointerException
 IOException
🔹 Example:
java
CopyEdit
try {
int[] arr = new int[3];
arr[5] = 100; // throws exception
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Index out of bounds");
}

✅ 4. Concurrent Programming
Concurrent programming allows multiple tasks (threads) to
run simultaneously.
🔹 Benefits:
 Better CPU usage
 Faster performance in I/O-heavy programs

✅ 5. The Thread Class and Runnable Interface


🔹 Creating a Thread (2 Ways):
Method 1: Extending Thread class
java
CopyEdit
class MyThread extends Thread {
public void run() {
System.out.println("Thread running");
}
}
Method 2: Implementing Runnable interface
java
CopyEdit
class MyRunnable implements Runnable {
public void run() {
System.out.println("Runnable running");
}
}
🔹 Flowchart:
sql
CopyEdit
Create Thread
|
Start Thread
|
run() method executes
|
Thread terminates

✅ 6. Thread Priorities
🔹 Java Thread Priorities:
 Thread.MIN_PRIORITY = 1
 Thread.NORM_PRIORITY = 5 (default)
 Thread.MAX_PRIORITY = 10
java
CopyEdit
Thread t1 = new Thread();
t1.setPriority(Thread.MAX_PRIORITY);

✅ 7. Synchronization
🔹 Problem: Race Conditions
Two threads accessing shared data can corrupt it.
🔹 Solution: synchronized block/method
java
CopyEdit
synchronized(obj) {
// critical section
}

✅ 8. Java’s I/O Streams


🔹 Types of Streams:
 Byte Streams: InputStream, OutputStream
 Character Streams: Reader, Writer

✅ 9. Byte Streams and Character Streams


Stream Type Base Class Used For
Byte Streams Input/OutputStream Binary data
Character Streams Reader/Writer Text data
java
CopyEdit
FileInputStream fin = new FileInputStream("file.txt"); // Byte
FileReader fr = new FileReader("file.txt"); // Char

✅ 10. FileWriter and FileReader


🔹 Example:
java
CopyEdit
FileWriter fw = new FileWriter("example.txt");
fw.write("Hello World");
fw.close();

FileReader fr = new FileReader("example.txt");


int ch;
while ((ch = fr.read()) != -1) {
System.out.print((char) ch);
}
fr.close();

📘 Summary Sheet (Quick Revision)


Topic Use/Concept
Logical grouping, access outer
Nested Classes
members
Exception Handling Manage runtime errors gracefully
Thread & Runnable Multitasking in Java
Avoids data corruption with multiple
Synchronization
threads
I/O Streams Reading/writing files in Java
FileWriter/FileReader Character-based file handling

You might also like