0% found this document useful (0 votes)
3 views41 pages

Module 3 Part 1

Module 3 of the Java programming course focuses on packages, exception handling, and multithreading, building on the foundational knowledge from Modules 1 and 2. It addresses the limitations of previous modules by introducing structured project organization, error management, and concurrent programming, essential for developing industrial-level applications. The module aims to equip students with the skills to create robust, maintainable, and high-performance Java systems.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views41 pages

Module 3 Part 1

Module 3 of the Java programming course focuses on packages, exception handling, and multithreading, building on the foundational knowledge from Modules 1 and 2. It addresses the limitations of previous modules by introducing structured project organization, error management, and concurrent programming, essential for developing industrial-level applications. The module aims to equip students with the skills to create robust, maintainable, and high-performance Java systems.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Module 3: Packages, Exception Handling and

Multithreading

Premanand S

Assistant Professor
School of Electronics Engineering
Vellore Institute of Technology
Chennai Campus
premanand.s@[Link]

March 2, 2026

Premanand S (VIT-CC) Module 3 March 2, 2026 1 / 37


Learning Progression in Java

Java programming is taught in a progressive and layered manner.


Each module addresses a specific level of software development
maturity.
Module 3 is introduced after fundamentals and OOP design are
established.

Progression:

Module 1 → Module 2 → Module 3

Premanand S (VIT-CC) Module 3 March 2, 2026 2 / 37


What We Achieved in Module 1

Understanding the Java programming model


JVM, bytecode, and program execution flow
Basic programming constructs:
Data types, variables, operators
Control and looping constructs
Arrays and strings
Writing syntactically correct Java programs

Limitation: Programs are small, sequential, and not scalable.

Premanand S (VIT-CC) Module 3 March 2, 2026 3 / 37


What Module 2 Added

Object-oriented design using:


Classes and objects
Inheritance and polymorphism
Abstract classes and interfaces
Code reusability and modularity
Real-world problem modeling

Limitation:
No standard project organization
No mechanism to handle runtime failures
Programs execute in a single thread

Premanand S (VIT-CC) Module 3 March 2, 2026 4 / 37


Why Module 3 Is Required Now

Real-world Java applications are:


Large
Error-prone
Concurrent
Object-oriented design alone is not sufficient
Application-level concerns must be addressed

Module 3 bridges the gap between:

Object-Oriented Design → Industrial Java Applications

Premanand S (VIT-CC) Module 3 March 2, 2026 5 / 37


Problems Without Module 3

Large number of classes become difficult to manage


Programs crash due to unhandled runtime exceptions
Inefficient CPU utilization due to sequential execution
Shared data inconsistencies in concurrent environments

Premanand S (VIT-CC) Module 3 March 2, 2026 6 / 37


How Module 3 Solves These Problems

Packages
Creating and accessing packages
Sub-packages and hierarchical organization

Exception Handling
Types of exceptions (Checked and Unchecked)
Control flow in exceptions
Use of try, catch, finally
Use of throw and throws
User-defined exceptions

Multithreading
Basics of threads
Defining threads using Runnable interface
Multiple threads execution
Inter-thread communication
Synchronization for data consistency

Premanand S (VIT-CC) Module 3 March 2, 2026 7 / 37


Why Module 3 Comes After Module 2

Packages require understanding of classes


Exceptions propagate through method calls
Threads operate on objects and shared resources

Hence:

Module 3 logically depends on Modules 1 and 2

Premanand S (VIT-CC) Module 3 March 2, 2026 8 / 37


Outcome of Module 3

Ability to build:
Structured Java projects
Robust and error-resilient applications
Concurrent and high-performance systems
Readiness for:
Industry-level Java development
Advanced Java frameworks

Premanand S (VIT-CC) Module 3 March 2, 2026 9 / 37


Why Do We Need Packages?

Premanand S (VIT-CC) Module 3 March 2, 2026 10 / 37


Why Do We Need Packages?

Premanand S (VIT-CC) Module 3 March 2, 2026 11 / 37


What is a Package?

Definition
A package in Java is a collection of related classes and interfaces grouped
together under a common namespace.

Acts like a folder in a file system


Provides a namespace to avoid naming conflicts
Helps in logical organization of code
Improves maintainability and reusability

Simple Analogy
Classes : Doctors
Package : Department

Premanand S (VIT-CC) Module 3 March 2, 2026 12 / 37


Why Do We Need Packages?

Without Packages:
All classes go into the default package
Naming conflicts may occur
Difficult to maintain large projects
No proper modularization
Limited control over accessibility (security issues)

With Packages:
Logical grouping of related classes
Avoid naming conflicts
Better access control
Code reusability
Improved maintainability

Premanand S (VIT-CC) Module 3 March 2, 2026 13 / 37


Syntax of Package in Java
Basic Syntax:
Package Declaration
package package_name;

Example:
Example
package hospital;

public class Doctor {


void display() {
[Link]("Doctor Class");
}
}

Premanand S (VIT-CC) Module 3 March 2, 2026 14 / 37


Step 1: Opening Command Prompt

Action:
Press Windows + R
Type cmd
Press Enter
Initial Location:

C:\Users\username>

Learning:
This is the user home directory.
All navigation starts from here.

Premanand S (VIT-CC) Module 3 March 2, 2026 15 / 37


Step 2: Checking Available Folders

Command Used:

dir

Purpose:
Lists all folders inside current directory
Helps locate Desktop folder
Observation:
Desktop was inside OneDrive

Premanand S (VIT-CC) Module 3 March 2, 2026 16 / 37


Step 3: Navigating to Desktop

Command Used:

cd OneDrive\Desktop

Now Location Becomes:

C:\Users\username\OneDrive\Desktop>

Learning:
cd means change directory
Windows may store Desktop inside OneDrive

Premanand S (VIT-CC) Module 3 March 2, 2026 17 / 37


Step 4: Creating Project Folder

Command Used:

mkdir JavaPackageDemo
cd JavaPackageDemo

Learning:
mkdir creates new folder
Always work inside a dedicated project directory

Premanand S (VIT-CC) Module 3 March 2, 2026 18 / 37


Step 5: Checking Java Installation

Command Used:

java -version

Error Observed:

’java’ is not recognized...

Learning:
Java was not installed
JDK installation is mandatory

Premanand S (VIT-CC) Module 3 March 2, 2026 19 / 37


Step 6: Installing JDK and Verifying
Download JDK From:
Website: [Link]
Version Used: OpenJDK 21 (LTS)
Platform: Windows x64 (.msi installer)

After Installation, Verify Using:

java -version
javac -version

Learning:
java runs the JVM (Java Virtual Machine)
javac compiles .java into .class files
JDK = JVM + Compiler + Development Tools
Premanand S (VIT-CC) Module 3 March 2, 2026 20 / 37
Step 7: Creating Package Structure

Command Used:

mkdir student

Learning:
Package name must match folder name
Java follows directory-based organization

Premanand S (VIT-CC) Module 3 March 2, 2026 21 / 37


Creating Java Files Using Notepad
Step 1: Create Package Class ([Link])
notepad student\[Link]

Notepad opens automatically.


Paste the [Link] code.
Click File 
Save.
Close Notepad.

Step 2: Create Main Class ([Link])


notepad [Link]

Notepad opens again.


Paste the [Link] code.
Click File 
Save.
Close Notepad.
Premanand S (VIT-CC) Module 3 March 2, 2026 22 / 37
[Link] (Inside Package)
package student;
public class Student {
String name;
int rollNo;
double marks;
public Student(String name, int rollNo, double marks) {
[Link] = name;
[Link] = rollNo;
[Link] = marks;
}

public void displayDetails() {


[Link]("Student Name: " + name);
[Link]("Roll Number: " + rollNo);
[Link]("Marks: " + marks);
} }
Premanand S (VIT-CC) Module 3 March 2, 2026 23 / 37
[Link] (Accessing Package)

import [Link];

public class MainApp {


public static void main(String[] args) {

Student s1 = new Student("Arun", 101, 89.5);


[Link]();
}
}

Premanand S (VIT-CC) Module 3 March 2, 2026 24 / 37


Step 8: Compilation Process

Compile Command:

javac student\[Link] [Link]

Learning:
Compiler generates .class files
Compilation must be done from project root

Premanand S (VIT-CC) Module 3 March 2, 2026 25 / 37


Step 9: Running the Program

Run Command:

java MainApp

Execution Flow:
JVM loads [Link]
Accesses [Link] from student folder
Program executes successfully

Premanand S (VIT-CC) Module 3 March 2, 2026 26 / 37


What Did We Learn from the Package Demo?

Packages are not just syntax


package student; is linked to folder structure and JVM
Folder structure must match package name
Package name = Directory name
Import is required for cross-package access
import [Link];
Access modifiers matter
Class must be public to access outside package
Compilation location is important
Compile from project root directory
Understanding JVM Class Loading
JVM searches for student/[Link]
Big Takeaway
Packages enable modular, maintainable, industry-level Java applications

Premanand S (VIT-CC) Module 3 March 2, 2026 27 / 37


Access Modifiers Across Packages

Access in Java depends on:


Access modifier (private, default, protected, public)
Whether classes are in same or different package
Whether inheritance is involved

Key Idea: Access control works across


Package boundaries
Inheritance hierarchy

Premanand S (VIT-CC) Module 3 March 2, 2026 28 / 37


Access Control Comparison Table

Modifier Class Same Diff. Pkg Diff. Pkg


Itself Package (Subclass) (Non-Subclass)
private ✓ × × ×
default ✓ ✓ × ×
protected ✓ ✓ ✓ ×
public ✓ ✓ ✓ ✓

Premanand S (VIT-CC) Module 3 March 2, 2026 29 / 37


Demo Structure

Project Structure:

project/
pack1/
[Link]
pack2/
[Link]

Premanand S (VIT-CC) Module 3 March 2, 2026 30 / 37


pack1/[Link]
package pack1;

public class A {

private int privateVar = 10;


int defaultVar = 20;
protected int protectedVar = 30;
public int publicVar = 40;

public void display() {


[Link](privateVar);
[Link](defaultVar);
[Link](protectedVar);
[Link](publicVar);
}
}
Premanand S (VIT-CC) Module 3 March 2, 2026 31 / 37
pack2/[Link] (Without Inheritance)
package pack2;

import pack1.A;
public class B {
public static void main(String[] args) {

A obj = new A();

// [Link]([Link]); // Error
// [Link]([Link]); // Error
// [Link]([Link]); // Error
[Link]([Link]); // Works
}
}

Conclusion: Only public is accessible across packages (no inheritance).


Premanand S (VIT-CC) Module 3 March 2, 2026 32 / 37
pack2/[Link] (With Inheritance)

package pack2;
import pack1.A;
public class B extends A {
public static void main(String[] args) {
B obj = new B();
// [Link]([Link]); // Error
// [Link]([Link]); // Error
[Link]([Link]); // Works
[Link]([Link]); // Works
}
}

Conclusion:
protected works with inheritance
public works always
Premanand S (VIT-CC) Module 3 March 2, 2026 33 / 37
Important Observations

default access is package-level only


protected allows subclass access across packages
private is strictly within the same class
public has no restriction
Sub-package is NOT same as parent package

Premanand S (VIT-CC) Module 3 March 2, 2026 34 / 37


Conceptual Understanding Questions

Why is default not accessible in different package?


Why does protected require inheritance?
What happens if class A is not declared public?
Is sub-package considered same package?

Key Takeaway:
Access modifiers define visibility across both package boundaries and
inheritance hierarchy.

Premanand S (VIT-CC) Module 3 March 2, 2026 35 / 37


Access Modifiers Across Packages – Comparison
1. Same Package

package pack1;
A obj = new A();
obj.x; // private
obj.y; // default
obj.z; // protected
obj.w; // public

Premanand S (VIT-CC) Module 3 March 2, 2026 36 / 37


Access Modifiers Across Packages – Comparison
1. Same Package 2. Different Package
(No Inheritance)
package pack1;
A obj = new A(); package pack2;
obj.x; // private import pack1.A;
obj.y; // default A obj = new A();
obj.z; // protected obj.y;
obj.w; // public obj.z;
obj.w;

Works:
y, z, w

Premanand S (VIT-CC) Module 3 March 2, 2026 36 / 37


Access Modifiers Across Packages – Comparison
1. Same Package 2. Different Package 3. Different Package
(No Inheritance) (With Inheritance)
package pack1;
A obj = new A(); package pack2; package pack2;
obj.x; // private import pack1.A; import pack1.A;
obj.y; // default A obj = new A(); class B extends A {
obj.z; // protected obj.y; B obj = new B();
obj.w; // public obj.z; obj.z;
obj.w; obj.w;
}
Works:
Works:
y, z, w
w only

Premanand S (VIT-CC) Module 3 March 2, 2026 36 / 37


Access Modifiers Across Packages – Comparison
1. Same Package 2. Different Package 3. Different Package
(No Inheritance) (With Inheritance)
package pack1;
A obj = new A(); package pack2; package pack2;
obj.x; // private import pack1.A; import pack1.A;
obj.y; // default A obj = new A(); class B extends A {
obj.z; // protected obj.y; B obj = new B();
obj.w; // public obj.z; obj.z;
obj.w; obj.w;
}
Works:
Works:
y, z, w
Works:
w only
z, w

Premanand S (VIT-CC) Module 3 March 2, 2026 36 / 37


Access Modifiers Across Packages – Comparison
1. Same Package 2. Different Package 3. Different Package
(No Inheritance) (With Inheritance)
package pack1;
A obj = new A(); package pack2; package pack2;
obj.x; // private import pack1.A; import pack1.A;
obj.y; // default A obj = new A(); class B extends A {
obj.z; // protected obj.y; B obj = new B();
obj.w; // public obj.z; obj.z;
obj.w; obj.w;
}
Works:
Works:
y, z, w
Works:
w only
z, w

private  Only inside class


default  Same package only
protected  Same package + Subclass
public  Everywhere
Premanand S (VIT-CC) Module 3 March 2, 2026 36 / 37
Thank You!
Stay Connected

Premanand S
Email: premanand.s@[Link]
Phone: +91-7358679961

LinkedIn: [Link]/in/premsanand
Instagram: [Link]/premsanand
WhatsApp Channel: anandsDataX

Google Scholar: Google Scholar Profile


GitHub: [Link]/anandprems

Premanand S (VIT-CC) Module 3 March 2, 2026 37 / 37

You might also like