0% found this document useful (0 votes)
10 views12 pages

Packages and Interfaces 1

The document provides an overview of Java packages and interfaces, explaining their purpose, access control, and the importance of organizing code. It details access levels in Java, including private, default, protected, and public, as well as the process of importing packages. Additionally, it covers exception handling in Java, including keywords like try, catch, finally, throw, and throws, with examples demonstrating their usage.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views12 pages

Packages and Interfaces 1

The document provides an overview of Java packages and interfaces, explaining their purpose, access control, and the importance of organizing code. It details access levels in Java, including private, default, protected, and public, as well as the process of importing packages. Additionally, it covers exception handling in Java, including keywords like try, catch, finally, throw, and throws, with examples demonstrating their usage.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Packages and

Interfaces

MODULE 4
Packages

A package is a namespace used to group related classes and interfaces logically. It's analogous to a
folder in a file system and is used to manage the organization of Java classes and interfaces into
different namespaces.

Purpose of Using Packages:

Organization: Helps organize classes and interfaces into different namespaces, making it easier to
manage and locate them.
Avoid Naming Conflicts: By using different namespaces, the same class or interface name can be used
in different packages without conflict.
Access Control: Packages can be used to control access to classes and their members. Members with
default (package-private) access are only accessible within the same package .
Access Protection in
Packages
Access protection in Java, particularly in the
context of packages and object-oriented
programming (OOP), revolves around
controlling the visibility and accessibility of
classes, methods, and other members (like
variables). Java provides several access
modifiers to set the access level for classes,
constructors, methods, and variables.
1. Private
Scope: Accessible only within the declared class. 4
Usage: Typically used for fields and methods that are intended for
use solely within the class itself (e.g., helper methods or internal
data members).

Different 2. Default (Package-Private)


Scope: Accessible only within the same package.
Usage: If no access modifier is specified, the member has default

Access access. This is often used for classes, interfaces, methods, and
fields that should be accessible within the same package but hidden
from external packages.
Levels in 3. Protected
Scope: Accessible within the same package and also accessible in

Java
subclasses of the class, even if they are in different packages.
Usage: Typically used when you want to allow a parent class to
expose certain properties or methods to its subclasses while
keeping them hidden from other classes and packages.
4. Public
Scope: Accessible from any other class, regardless of the package.
Usage: Public access is generally given to public APIs of a class -
those parts that are intended to be used by other classes and
clients, including methods for interaction with the object and certain
fields.
5
Importing Packages
Importing packages in Java is a fundamental aspect of Object-Oriented
Programming (OOP) and is key to organizing code efficiently and reusing existing
classes and interfaces. When you import a package, you make its classes and
interfaces available to the class you're writing, without having to specify their fully
qualified names each time you use them.

Syntax:

import package.name.Class; // Import a single class


import package.name.*; // Import the whole package
6
Example:
Here's a simple example demonstrating the use of imports:

import java.util.List;
import java.util.ArrayList;
public class Example {
public static void main(String[] args) {
List<String> myList = new ArrayList<>(); // Using
classes from java.util
System.out.println("List created: " + myList);
}
}

In this example, List and ArrayList from the java.util package are imported and used
without needing to specify their fully qualified names each time.
7
An interface in Java is a blueprint of a class. It
has static constants and abstract methods.
The interface in Java is a mechanism to achieve
abstraction. There can be only abstract methods
Interface in the Java interface, not method body. It is used
to achieve abstraction and multiple
s inheritance in Java.
In other words, you can say that interfaces can
have abstract methods and variables. It cannot
have a method body.
Java Interface also represents the IS-A
relationship.

Use of java interface:


It is used to achieve abstraction.
8
Example of Interface
interface printable{
void print();
}

class A6 implements printable{


public void print(){System.out.println("Hello");}

public static void main(String args[]){


A6 obj = new A6();
obj.print();
}
}
Exception Handling in 9

Java
The Exception Handling in Java is one of the powerful mechanism to handle
the runtime errors so that the normal flow of the application can be
maintained.

Exception Handling is a mechanism to handle runtime errors such as


ClassNotFoundException, IOException, SQLException, RemoteException, etc.

Dictionary Meaning: Exception is an abnormal condition.

In Java, an exception is an event that disrupts the normal flow of the program.
It is an object which is thrown at runtime.
Java Exception 10

Keywords
try
The "try" keyword is used to specify a block where we should place an exception code. It means we
can't use try block alone. The try block must be followed by either catch or finally.
catch
The "catch" block is used to handle the exception. It must be preceded by try block which means
we can't use catch block alone. It can be followed by finally block later.
finally
The "finally" block is used to execute the necessary code of the program. It is executed whether an
exception is handled or not.
throw
The "throw" keyword is used to throw an exception.
throws
The "throws" keyword is used to declare exceptions. It specifies that there may occur an exception
in the method. It doesn't throw an exception. It is always used with method signature
Java Exception Handling 11

Example:
public class JavaExceptionExample{
public static void main(String args[]){
try{
int data=50/0;
}catch(ArithmeticException e){System.out.println(e);}
System.out.println("rest of the code...");
}
}

Output:
java.lang.ArithmeticException: / by zero rest of the code.
THANK YOU
HINA SHAKYA
USN:- 4NI22CS082

You might also like