0% found this document useful (0 votes)
14 views

Packages&Interfaces

Java packages and interfaces notes

Uploaded by

Chaitra M Bhat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Packages&Interfaces

Java packages and interfaces notes

Uploaded by

Chaitra M Bhat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Chapter 9:

Packages and Intefaces


Chapter Topics
Chapter 9 discusses the following main topics:
⚫ Packages
⚫ Access protection
⚫ Importing packages
⚫ Interfaces
Packages
❖ A package is both a naming and a visibility control
mechanism:
1) divides the name space into disjoint subsets It is possible to
define classes within a package that are not accessible by code
outside the package.
2) controls the visibility of classes and their members It is
possible to define class members that are only exposed to other
members of the same package.
❖ Same-package classes may have an intimate knowledge of
each other, but not expose that knowledge to other packages
Packages

Package in java can be categorized in two form,


o built-in package and
o user-defined package.
There are many built-in packages such as java, lang, awt, javax,
swing, net, io, util, sql etc.
Creating a Package
•A package statement inserted as the first line of the
source file:
package myPackage;
class MyClass1 { … }
class MyClass2 { … }
•means that all classes in this file belong to the
myPackage package.
• The package statement creates a name space where
such classes are stored.
•When the package statement is omitted, class names
are put into the default package which has no name.
Finding Packages

⚫ As packages are stored in directories, how does the Java


run-time system know where to look for packages?
⚫ Three ways:
1) The current directory is the default start point - if
packages are stored in the current directory or
sub-directories, they will be found.
2) Specify a directory path or paths by setting the
CLASSPATH environment variable.
3)Use –classpath option with java and javac to specify the
path to classes
.
Example: Package

Save, compile :
1) Save the file as MyPackageClass.java
2) Compile file javac MyPackageClass.java
3) Compile package as javac -d . MyPackageClass.java
This forces the compiler to create the "mypack" package.
The -d keyword specifies the destination for where to save the class file

L 1.9
Importing Packages

There are three ways to access the package from outside the package.
1. import package.*;
2. import package.classname;
3. fully qualified name.
Using packagename.classname

Save, compile and Execute :


1) Save the file as test.java
2) Compile file javac test.java
3) Compile package as javac -d . test.java
4) Run the file as java pk.test
Using packagename.*
Using fully qualified name
• If you use fully qualified name then only declared class of this package
will be accessible. Now there is no need to import. But you need to use
fully qualified name every time when you are accessing the class or
interface.
• It is generally used when two packages have same class name e.g.
java.util and java.sql packages contain Date class.
Access Modifiers/Specifiers
• The access modifiers in java specify accessibility (scope) of a
data member, method, constructor or class.
•There are 3 types of java access modifiers:
1. private
2. protected
3. public

private access modifier The private access modifier is accessible


only within class.
protected access modifier
•The protected access modifier is accessible within package
and outside the package but through inheritance only.
•The protected access modifier can be applied on the data
member, method and constructor. It can't be applied on the
class.
public access modifier The public access modifier is accessible
everywhere. It has the widest scope among all other modifiers.
Access Modifiers/Specifiers
Interfaces
•An interface in java is a blueprint of a class. It has static final
variables and abstract methods.
•The interface in java is a mechanism to achieve abstraction.
There can be only abstract methods in the java interface does
not contain method body. It is used to achieve abstraction and
multiple inheritance in Java.
•It cannot be instantiated just like abstract class.
•Interface fields are public, static and final by default, and
methods are public and abstract.

•There are mainly three reasons to use interface. They are given
below.
•It is used to achieve abstraction.
•By interface, we can support the functionality of multiple
inheritance.
Example
interface printable public class IntefacePgm1
{ {
void print(); public static void main(String
} args[])
class Pgm1 implements printable {
{ Pgm1 obj = new Pgm1 ();
public void print() obj.print();
{ }
System.out.println("Hello"); }
}
}
Two classes implementing same interface
interface Drawable public class TestInterface1
{ {
void draw(); public static void main(String args[])
}
{
Drawable d=new Circle();
//Implementation: by second user
class Rectangle implements Drawable d.draw();
{ }
public void draw() }
{
System.out.println("drawing rectangle");
}
}

class Circle implements Drawable


{
public void draw()
{
System.out.println("drawing circle");
}
}
Multiple inheritance in Java by interface
•If a class implements multiple interfaces, or an interface
extends multiple interfaces i.e. known as multiple inheritance.
Example

interface Printable class InterfaceDemo


{
void print(); {
} public static void main(String args[])
interface Showable {
{ InterfacePgm1 obj = new InterfacePgm1
void print(); ();
} obj.print();
class InterfacePgm1 implements }
Printable, Showable }
{
public void print()
{
System.out.println("Hello");
}
}
Interface Example
TRY

Write a program to create an Interface USB with a method


getName(). Create two subclasses mouse and pendrive that
implements the interface.

Create tester class to test these methods.


Thank You

You might also like