Java Unit 3 Complete Notes – Exception Handling, Inheritance, Packages, and Interfaces.

 


Java unit 3 notes 


📖 Java Unit 3 Notes – Complete Explanation with Examples

In this post, we will cover the core topics of Java Unit 3 including exception handling, inheritance, abstract classes, interfaces, and packages. Each concept is explained clearly with definitions, syntax, examples, and key differences — perfect for students and beginners in Java programming.


🧨 1. What is Exception Handling?

Exception Handling in Java is a mechanism that deals with runtime errors. It allows the program to handle errors gracefully without crashing the application. Java provides five key elements for exception handling:

  • try

  • catch

  • throw

  • throws

  • finally

These tools help ensure smooth program execution and improve fault tolerance.


✍️ 2. Syntax of Exception Handling in Java

try { // Code that might throw an exception } catch(ExceptionType e) { // Code to handle the exception } finally { // Optional block; always executes }

⚠️ 3. What are Uncaught Exceptions?

Uncaught exceptions are those that are not handled using try-catch blocks. When they occur, the Java Virtual Machine (JVM) terminates the program and prints an error.

Example:

public class Example { public static void main(String[] args) { int a = 10 / 0; // Causes ArithmeticException } }

📦 4. What is a User-Defined Package?

A user-defined package is a custom package created by the developer to organize related classes and interfaces. It helps in:

  • Code organization

  • Avoiding name conflicts

  • Improving reusability

To define a package:

package mypackage;

👨‍👦 5. Short Note on Subclass

A subclass is a child class that inherits properties and methods from a superclass using the extends keyword. It can also override methods and define its own.


🚘 6. Superclass with Example

A superclass is the parent class from which a subclass inherits fields and methods.

Example:

class Vehicle { void run() { System.out.println("Vehicle is running"); } } class Car extends Vehicle { // Inherits run() method }

🔁 7. What is Inheritance?

Inheritance is an object-oriented concept where a class (child/subclass) inherits the properties of another class (parent/superclass). It promotes:

  • Reusability

  • Polymorphism

  • Code maintenance

Java supports single inheritance using the extends keyword.


✨ Short Answer Questions


1️⃣ What is Superclass and Subclass?

  • Superclass: A general class whose features are inherited by other classes.

  • Subclass: A specialized class that inherits from the superclass and can override or extend its behavior.


2️⃣ Abstract Class vs Interface

FeatureAbstract ClassInterface
Keywordsabstractinterface
InheritanceSingleMultiple
Method TypesAbstract + ConcreteAbstract (default/static allowed)
VariablesInstance variables allowedOnly public static final
ConstructorsAllowedNot allowed

3️⃣ How to Create and Implement an Interface?

Step 1: Define Interface

interface Drawable { void draw(); }

Step 2: Implement in Class

class Circle implements Drawable { public void draw() { System.out.println("Drawing Circle"); } }

Step 3: Use in Main

public class Main { public static void main(String[] args) { Drawable d = new Circle(); d.draw(); } }

4️⃣ What is an Interface?

An interface is a collection of abstract methods. It defines a contract that implementing classes must follow. All methods are implicitly public and abstract. Interfaces allow multiple inheritance and abstraction.


5️⃣ Class vs Interface

FeatureClassInterface
ContainsFields, methods, constructorsOnly method declarations & constants
InheritanceSingleMultiple
ImplementationCan be instantiatedMust be implemented by class

6️⃣ Types of Packages in Java

  1. Built-in Packages: Provided by Java like java.util, java.io, java.lang.

  2. User-Defined Packages: Created by the programmer using the package keyword.


7️⃣ Example of Multiple Catch Blocks

public class MultipleCatchExample { public static void main(String[] args) { try { int[] arr = new int[5]; arr[10] = 50; // ArrayIndexOutOfBoundsException int result = 10 / 0; // ArithmeticException } catch (ArrayIndexOutOfBoundsException e) { System.out.println("Array index is out of bounds."); } catch (ArithmeticException e) { System.out.println("Cannot divide by zero."); } catch (Exception e) { System.out.println("Other exception occurred."); } } }

8️⃣ What is Rethrowing an Exception?

Rethrowing means catching an exception and throwing it again to pass it up the call stack. This is useful for logging and layered exception handling.

try { // some code } catch (IOException e) { log(e); throw e; // rethrows to be handled higher up }

9️⃣ Concept of Exception Handling in Java

Java uses a robust exception handling model involving:

  • try: Wraps risky code

  • catch: Handles specific exceptions

  • throw: Manually throw an exception

  • throws: Declare exceptions that a method can throw

  • finally: Always executes (cleanup code)

This ensures smooth execution and better program stability.


📚 Final Words

Understanding concepts like exception handling, inheritance, interfaces, and packages is crucial for mastering Java. These form the core of object-oriented programming and are frequently asked in interviews and exams.

If you found this helpful, don’t forget to bookmark this post for quick reference. ✅

Post a Comment

0 Comments