ws
me Interfaces
_ /
ductio! ort multiple inheritance. But, a large numb:
t ooh use of multiple inheritance. Therefore
ee known as interfaces to support the ci
pro:
| wt es 0
* tions
gers 2 7 ,
neta in java is also a mechanism to achieve fully abstraction. Interface is
He interfac f abstract data members such as methods. All th
colection “implicitly public and abstract. The method defined
interface rit implementation and only specify the Parameters
potnave thel lues they will return. An interface is always imple
weype of val lementing an interface must define alll the metho:
The class ae Java compiler given an error Message. Interface in Java is
7 nent to an abstract base class. You cannot instantitate a
equ
iN Object through an
terface, but you can offer a set of functionalities that is common to Several different
inter ’
classes.
er of real-life
» Java provides an
Oncept of multiple
'e Members of an
in an interface do
they will take and
‘Mented in a class.
ds contained inside
What is an Interface?
Inleface looks like class but it is not a class, An interface can have methods and
Variables just like the class but the methods declared in interface at by default
abstract (
(only methed signature, no body). Also, the variables declaredin an: interface
ae public, static & final by default. *
Interfaces are used for abstraction. Since methods in interfaces does not have
body, they have to be
implemented by the class before you can access ee
set bstinplenis interface must mpl al enna aes
FS leva Programming language does not support multiple os
Mace We can achieve this as a class can implement more than one interlac
—___ ES IS a5 a [Link]
[257]
Lay public and a
a autatical PUDIE NS Roane
ey pod and public i:
oe eth
static and fing)”
abstraction”
wt the functionality of multiple inheritangs
or
variables declaration;
methods declaration;
interface is keyword
. \
interfacename is any valid java identifier... \
are, as
ds are declared using only their return type and ‘signature. They
. We know now that in an interface, no method can have animplemet=
vi es declared in the interface definition are constant, therefore, te!®
tiaized with a constant value. pysictoc 69a compiler adds automatically public a,
face method and public, oman a) /
and fi
Wi
Ee Note:
‘The jav'
pefore the inte!
before data members.
| s
ee "4
Gt me
oe
nterface
e interface.
98 Reasons to-use I
re three reasons to ust
There al
4. Itis used to achieve fully abstraction.
2. By interface, we can support the functionality of multiple inh |
eFitang
2,
3, Itcan be used to achieve loose coupling.
9h Defining an Interface
Interfaces are syntactically similar to classes.
Syntax is : 3 iE i
oh opt
/| Interface interfacename
ent guasaat en
variables declaration;
methods declaration;
)
Where
interface is keyword
\
interfé
facename is any valid java identifier. “
Methods are de
clan
methods. We = 7 sing ont tert ype ania. Tee 4
ow that in an interface, no method can have anime
2 All variables d
leclared in the
it
be initialized with a c otatant we definition are are constant, therefore, they
value. SoaSosentis te name of nee, cons
es ntai
oe 9 40 and one method displ tains one constant num
rr nethod display( ). Interface varia
cd fil mabe ee Public an eomcan are
as shown
Interface student
int num =
void display);
}
[Link]
Interface student
tg final int rum = 40;
public abstract void display( }
7959]J
Aortorences between Class and Interface
Differences between class and interface are shown in table 9 1
TABLE 9.1
Class
ntertacg
Class can have abstract and non-
abstract methods.
Class doesn't support, multiple
inheritace.
Class can have final, r
Static'and non-static variables.
It Gan use various access
specifiers like public, or protected.
A class implements the interface.
Class can have static methods,
main method and constructor.
n-final,
It can be instantiated by declaring
objects. objects. It can -
4 ee Only by
ve" age for further classifica 98s, i.e,
we%eckage ation an Categonmane® Within a
? i Of classes
eee
| 5”
Package call
and interlace
acka: —
; pa ge mypkg.mypkg2.m: 7
5 mypkg2 is a subpackage in package os 'ypkg3;
yPKg,
‘age myPkg2. .
se eg mypkgs isa ‘Subpackage in
te
ith
example 10.4
ne statement
package pel pek2.pck3;
spas a hierarchy of packages. The pek2 is a
a | . is a subpack
ig a subpackage in subpackage pck2. To Toler fo acess of a fee ane
ickage
| peed us
wecan use a fully qualified nai ith-all the containirig package names pret
s prefiing ihe
cass name as shown in figure 10.2.
omens [wD
Gx LD
package [Link] |
.ge Hierarchy]
tified by the
FIGURE 10.2 [Packa
bth
‘abo
ve package hierarchy class Ais der
\
\
|
|
|
\
V
Is
] 'S call
| led the\fully qualified type name of the classof Adding a Class to package
We can add a class t
0 a package using the following syntay
package packagename;
public class classname
{
11 body of class
}
Example 10.5
Step1 : -“Write the following code
package mypack;
2 public class Sample
{ ae
a
{
[Link] printin("Welcome to Packager
eee ; :
|
public static void main (String rgs{ })
} oe
Step 2: Save this file as [Link] . }
Step 3: Compile java package us
for example
Step 4:
1g following syntax :
Javac-d directory ' javafilename
ar agers 2 ,
Javac -d . [Link]
The -d switch specifies the destination where to
1 cifiés the put the ger
file. We can use any directory name like / home (in case etna
(in case of windows) etc. If we want to keep the pack ithir
directory, you can use . (dot). pin; package wintery
Run java package program
We need to use fully qualified name e. i
.g. [Link] to run thects
Command to run java package is : ypack-simple on
Jaya’ [Link] *
Output:
Welcome to package
ce a
peat a eaevery java source ae
0s es, 6 file Contaii
also 00 :
contain non- Public Class, at Teast
can ® kage. The name of th = bt ut ne ag
fH rape act 1° Source fi
ie ie with java extension, ie sh
a 6
40-
ra Kd;
aoe
oom
{groin
string name;
public Student(intmo, String sname)
rollno=mo;
name = sname;
}
public void showDetails()
{
[Link] printin("Roll No. is" +rolin);
[Link]("Nameis" +name);
}
public class DemoPackage
{
™ static void main(String ar[])
Student st{}=new Student{2];
t[0] =new Student(1001,"Amit");
st{1] =new Student(1002,"Shivank");Create a directory named as pack’ and Save the progran,
as
in this directory: *
ommand on the co!
Compile the code with the o "MAN romp
javac -d . [Link]
To run write the command given below :
java [Link]
Output:
Roll No. is 1001
Nameis Amit
Roll No. is 1002
Nameis Shivank
We can add number of classes in a package. These class
-member of the package.
Example 10.7
In this example, first create a package areapkg and adg
Rectarea and Circlearea to it.
First source File
package areapkg;
public class Rectarea
{
public double area (double /, double b)
{
return /* b;
Pe
.
il
Create a directory areapkg and save the file Rectarea”
and compile it.
Mop,
8 ate a
two Classes
‘sy,Fo JF SECONE SOUCE Fila
package areapkg,
public class Circleareg
{
public double ,
Categ Mondie ")
{
return 3.14% re
re
} ¥
i
2 Note : Ti
= Save this file ag itclearea, ji ~ -
lav;
| areapkg and compile it, 4 Sore i inthe directory
ol I
. TT sss LG
similarly, we can add any Number of Classes in, 8 Package
\6Using Package Members
These are three ways to access the Package Members from Outside the packa
Package:
e import packages; i
(ob) import [Link];
(c) fully qualified name
@) Using Package.* .
\fwe use Package.” then all the classes and
accessible but not Subpackages. The import keyword is used to make the
interfaces of this package will be
Class i yf ible to the current package.
ses and interface of another Package accessible Pi
[281]
_