Packages and Interfaces 1
Packages and Interfaces 1
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.
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).
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 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.
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.
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