Packages, Interfaces & Exceptions
Organizing code with packages and CLASSPATH, mastering Java access protection, designing contracts with interfaces, building fault-tolerant programs using Java's exception hierarchy and custom exceptions, and implementing Singleton & Adapter design patterns.
throw vs throws vs finally, custom exceptions, and Singleton & Adapter patterns.Section 1: Packages
Packages: Need, Defining a Package, Directory Hierarchy & CLASSPATH
Imagine two developers in a large project both create a class named Account or Student. Without namespaces, their .class files collide and overwrite each other! Java solves both naming collisions and fine-grained visibility control through Packages, backed by the CLASSPATH mechanism so javac and the JVM always know where to locate packaged .class files and .jar libraries.
Access Protection Matrix Across Packages & Importing Packages
Classes and packages collaborate to define Java's complete access-protection model. In Module 3 exams, drawing the 4×4 Access Protection Table (plus Same Class = 5th column) across packages p1 and p2, and knowing how import resolves class names and wildcard collisions, are guaranteed high-value topics.
Section 2: Interfaces
Interfaces: Defining, Implementing, Interface References & Partial Implementations
An Interface takes abstraction to the next level by separating what a class must do from how it does it—completely detached from any class hierarchy! Even two totally unrelated classes (say Printer and Invoice) can implement the exact same Printable interface and be manipulated polymorphically through a single Printable reference.
Extending Interfaces, Multiple Inheritance via Interfaces & Interface vs Abstract Class
Interfaces don't just live alone—an interface can inherit from one or multiple other interfaces using extends, and a single class can implement multiple interfaces simultaneously, cleanly giving Java safe multiple inheritance! Comparing Interfaces vs Abstract Classes is also one of the single most frequently tested comparison questions in KTU exams.
Section 3: Exception Handling
Errors vs Exceptions, Exception Hierarchy, Checked vs Unchecked & Default Handler
Even cleanly compiled Java programs face unexpected runtime troubles—dividing by zero, reading a missing file, accessing an invalid array slot, or losing a database connection. Without Exception Handling, the JVM abruptly crashes the entire application. Understanding Java's Throwable class hierarchy and the vital distinction between Checked and Unchecked exceptions is top-priority exam territory.
`try`, Multiple `catch` Clauses, Nested `try` Statements & `finally` Execution Flow
Once you understand what exceptions are, you need complete mastery over Java's runtime exception control flow: guarding blocks with try, ordering multiple catch clauses safely, unwinding nested try blocks, and guaranteeing cleanup using finally. Tracing try-catch-finally outputs is a staple KTU Part A and Part B question.
`throw`, `throws`, Exception Propagation, Built-in & Custom Exceptions
So far, the JVM created and threw exceptions automatically (10 / 0). What if your own business logic detects a problem—such as an underage voter, an invalid negative withdrawal amount, or an empty balance—and wants to reject it cleanly? Java provides **throw (to throw an exception explicitly), throws (to delegate checked exceptions up the call chain), and Custom Exceptions** (extending Exception for domain-specific errors!).
Section 4: Introduction to Design Patterns
Introduction to Design Patterns & the Singleton Design Pattern
Experienced software architects noticed that the same design headaches—like 'how do we guarantee only ONE shared database connection pool or configuration manager exists?'—keep recurring across completely different software projects. Instead of reinventing fragile workarounds, Design Patterns capture proven, battle-tested blueprints for structuring OOP classes. Module 3 focuses on two classic GoF patterns: Singleton and Adapter.
Adapter Design Pattern: Mechanism, Java Implementation & Singleton vs Adapter
Suppose your existing Java application expects every payment gateway or audio player to implement a clean interface (MediaPlayer.play()), yet you just acquired a powerful third-party legacy library whose method names are completely incompatible (AdvancedPlayer.playMp4()). You can't rewrite the third-party library, and rewriting your entire app would be risky. The Adapter Design Pattern solves this incompatibility effortlessly!