Vous êtes sur la page 1sur 9

Interfaces and Packages

 Packages
Introduction
Declaration of a package
Importing a package
 Interfaces
Introduction
Declaration of an interface
Accessing an interface

1
What is a Package?
 Package is considered as collection of several classes,
interfaces and sub packages; in turn each sub package
can have classes and interfaces.
 The classes defined within a package cannot be accessed
outside of it.
 Hence we can have classes of same name put in different
packages, so that they do not collide with each other.

2
Creating Packages
 SYNTAX:
Package identifier;
public class class-name
{
--------
}
 The creation of a package involves the following steps:
1)Create a directory,which has the same name as the package.
2)Declare the package at the beginning of the file using the form
package packagename;
3)Define the class,that is to be put in the package an declare it public.
public class <classname>
{
}
4)Save the java program in the directory created as classname.java
5)Compile the file
6)Change to previous level directory and use
Java packagename.classname

3
Importing a package
•• The
Thegeneral
generalsyntax
syntaxtotoimport
importaapackage
packageisis
import
import packageName.<className
packageName.<classNameor orinterface
interfacename>;
name>;
•• IfIfwe
weneed
needto toimport
importall
allthe
theclasses
classesand
andinterfaces
interfaceswewe
need
needtotouse
use**in inplace
placeof
ofclassname
classnameor orinterface
interfacename.
name.
import java.<packagename>.*;
import java.<packagename>.*;
•• Using
Using“*”
“*”will
willallow
allowyou
youtotoaccess
accessall
allthe
theclasses
classesand
and
interfaces
interfacesonly
onlyatatthat
thatlevel,the
level,thecontents
contentsof ofsubpackages
subpackages
will
willnot
notbe
beaccessible.To
accessible.Togian gianthe
theaccess
accessofofeach
eachsub
sub
package
packagethenthenthe
thecomplete
completepathpathupto
uptothat
thatlevel
levelmust
mustbebe
specified.
specified.

4
What is an Interface?
 In English, an interface is a device or system that unrelated
entities use to interact. According to this definition, a
remote control is an interface between you and a television
set, the English language is an interface between two
people, and the protocol of behavior enforced in the military
is the interface between people of different ranks. Similarly,
a Java interface is a device that unrelated objects use to
interact with one another. Java interfaces are probably most
analogous to protocols (an agreed-upon behavior). In fact,
other object-oriented languages have the functionality of
Java's interfaces, but they call their interfaces protocols.
 An interface is a named collection of method definitions
(without implementations). An interface can also include
constant declarations.

5
Declaration of an interface
 The general form of an interface is
Access interface interfacename
{
return type method name1(parameter-list);
return type method namen(parameter-list);
type final-varname1=value;
type final-varnameN=value;
}
Where access can be either public or default.
 When it is declared as public,the interface can be used by
any other code.Then,all methods and variables are
implicitly public.

6
Declaration of an interface
 When no access specifier is specified, the interface is only
available to other methods of the package in which it is
declared.
 Name is the name of the interface and it can be any valid
identifier.
 The methods are declared without any bodies, they end
with a semi-colon after the parameter list. Hence they are
abstract methods.
 There can be no default implementation of any method
specified within an interface.
 Each class that includes an interface should implement al
of the methods.
 Variables can be declared inside of interface declaration,
but they are implicitly final and static, they cannot be
changed by the implementing class. They should be
initialized with a constant value.

7
Interface implementation
 Once an interface has been defined,any number of classes
can implement that interface.
 To implement an interface,we have to include the
implements clause in a class definition and then add the
methods defined by the interface.
 The general form of a class with the implements clause is
access class classname[extends superclass]
[implements interface,[interface..]]
{
}
 The methods that implement an interface should be
declared public.
 The type signature of the implementing method should
match exactly with the type signature in the interface
definition.

8
Interface implementation
 Java allows the classes that implement interfaces to define
additional members of their own.
 If a class implements more than one interface,then he
interfaces should be separated with a comma.
 If a class implements two interfaces that declare the same
method,then the same method will be used by both
interfaces.
 If a class does not implement all the methods then it
should be declared as abstract.

Vous aimerez peut-être aussi