0% found this document useful (0 votes)
73 views20 pages

18CS653 Module 4

Uploaded by

bneelagund
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)
73 views20 pages

18CS653 Module 4

Uploaded by

bneelagund
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/ 20

PROGRAMMING IN JAVA

18CS653
MODULE-4
PACKAGES , INTERFACE ,
EXCEPTION HANDLING
TOPICS

Chapter: 09 Packages and Interfaces

Packages
Defining a Package
Finding Packages and CLASSPATH
A Short Package Example
Access Protection
An Access Example
Importing Packages
Interfaces
Defining an Interface
Implementing Interfaces
Nested Interfaces
Applying Interfaces
Variables in Interfaces
Interfaces Can Be Extended
TOPICS

Chapter: 10 Exception Handling

Exception-Handling Fundamentals
Exception Types
Uncaught Exceptions
Using try and catch
Displaying a Description of an Exception
Multiple catch Clauses
Nested try Statements
throw
throws
finally
Java’s Built-in Exceptions
Creating Your Own Exception Subclasses
Chained Exceptions
Using Exceptions
Packages

Definition: Package acts as a container. In Java package is used to group


related classes.

Example: In Java we can create two files with same name in different
packages.

Packages are used to avoid name conflicts, and to write a better maintainable
code.

Packages are divided into two categories:


Built-in Packages ( Packages from the java API )
User-defined Packages ( Create your own packages )
Defining a Package

To create a package we simply include a package command as the first


statement in a Java source file.
Any classes declared within that file will belong to the specified package.
The package statement defines a name space in which classes are stored.
If you omit the package statement, the class names are put into the default
package, which has no name.

This is the general form of the package statement:


package pkg;

Example:
package myPack; // myPack is the name of the package
we can create a hierarchy of packages. To do so, simply separate each package
name from the one above it by use of a period.

The general form of a multileveled package statement is shown here:


package pkg1[.pkg2[.pkg3]];

For example, a package declared as

package java.awt.image;
Finding Packages and CLASSPATH

First, by default, the Java run-time system uses the current working directory
as its starting point. Thus, if your package is in a subdirectory of the current
directory, it will be found.
Second, you can specify a directory path or paths by setting the CLASSPATH
environmental variable.
Third, you can use the -classpath option with java and javac to specify the
path to your classes.
Example
Access Protection

Packages act as containers for classes and other subordinate packages.


Classes act as containers for data and code.

Java addresses four categories of visibility for class members:


• Subclasses in the same package
• Non-subclasses in the same package
• Subclasses in different packages
• Classes that are neither in the same package nor subclasses
The three access specifiers, private, public, and protected, provide a variety
of ways to produce the many levels of access required by these categories.

The below table shows the sum of interactions


When the members are declared as private they can be accessed only
within that class.
When the members are declared as public they can be accessed anywhere.
When the members are declared as protected they can be in the same
package as well as in the different packages.
 When the members are defined without any access specifiers then they are
accessed in the same package subclass and non subclass.
Importing packages

To import the java package into a class, we need to use the java import
keyword
import keyword is used to access the package and its classes into the java
program.
We use import to access built-in and user-defined packages into our java
source file to refer to a class in another package by directly using its name.

syntax:
import package.name.ClassName; // To import a certain class only
import package.name.* // To import the whole package

Example:
import java.util.Date; // imports only Date class
import java.io.*; // imports everything inside java.io package
Example
Interfaces
interface keyword is used to make a class fully abstract
 interface specify that a class must do ,but, not how it does it.
interfaces are similar to classes but, they lack instance variable.
Methods in the interface are declared without body.
The classes that implement the interfaces implement the methods that are
declared in the interface.
The classes that implement the interface can also have their own methods.
One class can implement any number of interfaces.
Defining an Interfaces
An interface is defined much like a class. This is the general form of an
interface:

access interface name


{
return-type method-name1(parameter-list);
return-type method-name2(parameter-list);
type final-varname1 = value;
type final-varname2 = value;
// ...
return-type method-nameN(parameter-list);
type final-varnameN = value;
}

When no access specifier is included, then default access results, and the
interface is only available to other members of the package in which it is
declared.
Defining an Interfaces
Variables can be declared inside of interface declarations. They are
implicitly final and static, meaning they cannot be changed by the
implementing class.
They must also be initialized.
All methods and variables are implicitly public.
Example:

interface Callback
{
void callback(int param);
}
Implementing an Interfaces
Once an interface has been defined, one or more classes can implement
that interface.
To implement an interface, include the implements clause in a class
definition, and then create the methods defined by the interface.
The general form of a class that includes the implements clause looks like
this:
class classname [extends superclass] [implements interface [,interface...]]

{
// class-body
}
If a class implements more than one interface, the interfaces are separated
with a comma. If a class implements two interfaces that declare the same
method, then the same method will be used by clients of either interface.
Example
Example
Nested Interface
 An interface, i.e., declared within another interface or class, is known as a
nested interface.
Nested interface can be declared as private , protected or public.
When a nested interface is used outside of its enclosing scope, it must be
qualified by the name of the class or interface of which it is a member.

You might also like