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

Java Module 5

The document provides an overview of key Java concepts including packages, interfaces, access protection, exceptions, and exception handling mechanisms. It explains the definitions, types, benefits, and examples of each concept, such as the use of try-catch blocks and nested interfaces. The content is structured to facilitate understanding of Java programming principles and their practical applications.

Uploaded by

sachinsbs1913
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)
0 views9 pages

Java Module 5

The document provides an overview of key Java concepts including packages, interfaces, access protection, exceptions, and exception handling mechanisms. It explains the definitions, types, benefits, and examples of each concept, such as the use of try-catch blocks and nested interfaces. The content is structured to facilitate understanding of Java programming principles and their practical applications.

Uploaded by

sachinsbs1913
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

BPLCK105/205 C

MODULE 5

1. What is a Package? Explain with


example.
Ans:

Definition:
A package in Java is a namespace that organizes a set of
related classes and interfaces. It helps avoid name conflicts
and controls access with protection levels.

Types of Packages:

 Built-in packages: Provided by Java, e.g., java.lang, java.util,


java.io.

 User-defined packages: Created by the user for better


project structure.

Benefits:
 Code reusability
 Better namespace management
 Easy to locate classes and interfaces

Creating a Package:

// Save as MyClass.java
package mypack;

public class MyClass {


public void show() {
System.out.println("Welcome to Packages!");
}
}
Using a Package:

import mypack.MyClass;

public class Test {


public static void main(String args[]) {
MyClass obj = new MyClass();
obj.show();
}
}

2. Demonstrate the working of Interface


in JAVA.
Ans:

Definition:
An interface in Java is a blueprint for a class. It contains
abstract methods that must be implemented by classes.

Syntax:
interface InterfaceName {
returnType methodName();
}

Example:
interface Drawable {
void draw();
}

class Circle implements Drawable {


public void draw() {
System.out.println("Drawing Circle");
}
}
public class Test {
public static void main(String args[]) {
Drawable d = new Circle();
d.draw();
}
}

Features:
 Interfaces support multiple inheritance.
 From Java 8 onward, interfaces can have default and static
methods.

3. Explain the concept of Access


Protection in JAVA
Ans:

In Java, access protection refers to controlling the visibility of


classes, methods, and variables to ensure encapsulation,
security, and modularity. Java provides four levels of access
control using access modifiers: private, default (no modifier),
protected, and public.

1. private Access:
 The member is accessible only within the class in which it is
declared.
 It cannot be accessed from outside the class, not even from
subclasses or other classes in the same package.

Example:
class A {
private int x = 10;
}

2. Default Access (Package-private):


 If no access modifier is specified, it's called default access.
 The member is accessible only within classes of the same
package, but not from subclasses in other packages.

Example:

class A {
int x = 10; // default access
}

3. protected Access:
 The member is accessible within the same package and also in
subclasses outside the package.
 It is mainly used in inheritance.

Example:

class A {
protected int x = 10;
}

4. public Access:
 The member is accessible from any other class, regardless of
the package.
 It's the least restrictive access level.

Example:

public class A {
public int x = 10;
}

4. What is an Exception? Give an


example.
Ans:
Definition:
An exception is an abnormal condition that disrupts the normal
flow of the program.

Types:
 Checked Exception: Handled at compile time (e.g.,
IOException)
 Unchecked Exception: Handled at runtime (e.g.,
ArithmeticException)

Example:

public class Test {


public static void main(String args[]) {
try {
int a = 5 / 0;
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero");
}
}
}

Importance:
 Prevents program crash
 Allows graceful error handling

5. Explain the try and catch block with


example.
Ans:
Definition:
try and catch are used to handle exceptions in Java.

Syntax:
try {
// Code that might throw exception
} catch (ExceptionType name) {
// Code to handle exception
}

Example:

public class TryCatchExample {


public static void main(String args[]) {
try {
int x = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Exception caught: Division by
zero");
}
}
}

Use:
 Detect and handle run-time errors
 Avoids abnormal program termination

6. Explain types of Exception. Explain


different mechanisms of handling the
exception in Java.
Ans:

Types of Exceptions:

1. Checked Exceptions
o Detected during compilation
o Must be handled explicitly

o Example: IOException, SQLException


2. Unchecked Exceptions
o Occur at runtime

o Example: NullPointerException, ArithmeticException

Exception Handling Mechanisms:

Java provides five key mechanisms for handling exceptions:

1. try block:
It contains the code that may throw an exception.

2. catch block:
It is used to catch and handle the exception thrown from the try
block.

3. finally block:
This block always executes after try and catch, regardless of
whether an exception occurred or not. It is used to close
resources.

4. throw keyword:
Used to explicitly throw an exception from a method or block.

5. throws keyword:
Used in method signatures to declare that a method may throw
one or more exceptions.

Example combining these:

public class Example {


public static void main(String args[]) {
try {
int a = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero.");
} finally {
System.out.println("This will always run.");
}
}
}

7. Explain Nested Interfaces with


example.
Ans:

Definition:

A nested interface is an interface declared within another class


or interface.

Types:
 Interface inside interface
 Interface inside class

Example:
interface Outer {
void display();
interface Inner {
void show();
}
}

class Test implements Outer.Inner {


public void show() {
System.out.println("Nested Interface Example");
}

public static void main(String args[]) {


Outer.Inner obj = new Test();
obj.show();
}
}
Use Cases:
 For grouping functionality
 For hierarchical interface design

You might also like