Packages Applet
Packages Applet
1. pre-defined packages
1. Pre-defined packages : These packages comes with the java library . Some of the
packages are listed below :
Packages Description
(vi) java.net This package contains the classes for the network
and socket programming .
(vii) java.util This package contains the classes for the common
utilies program etc.. Calendar ,
Random etc..
etc.........
In order to define your own package , we have to include the following statement
as the first statement of your program
The statement is ,
package packagename;
package p;
class Sample
{
int i,j;
i=a;
j=b;
}
System.out.println("j="+j);
}
public static void main(String args[ ])
{
ob.setij(6,7);
ob.show();
}
}
1. First create the folder with name p suppose in the d: drive , so the path
will be d:\p
2. Now , save the file with the name Sample.java in the folder d:\p
Move to dos ,
c:\windows\desktop>_
c:\windows\desktop>d:
d:\>cd p
d:\p>
4. Now , compile the program
d:\p>javac Sample.java
5. To execute , move to its parent directory
d:\p>cd..
d:\>java p.Sample
We have created a folder with name packall in the drive d:\ and in this we have to
save all the .java files.
package packall;
class Factorial
{
void factorial(int num)
{
int fact=1,i;
for(i=1;i<=num;i++)
{
fact=fact*i;
}
System.out.println("Factorial = "+fact);
}
}
package packall;
class EvenOdd
{
void check(int num)
{
if(num%2==0)
System.out.println("Even.");
else
System.out.println("Odd.");
}
}
package packall;
class All
{
public static void main(String args[ ])
{
int num;
num=Integer.parseInt(args[0]);
f.factorial(num);
e.check(num);
}
}
d:\>cd packme
d:\pacme>javac *.java
d:\packme>cd ..
d:\>java packme.UseAll 5
Using the Multiple packages :
When we have to make use of the multiple packages then we have to make use of
the import statement .
import packagename.Classname;
or
import packagename.*;
The first form is used to import the particular class from a package
And the second form is used to import all the classes of the particular package .
eg...
Scenario : We have to create the three package with name p1,p2 and p2 in the
directory packall and in p1 we will create the file Factorial.java , in p2
EvenOdd.java and in p3 UseAll.java
package p1;
public class Factorial
{
public void factorial(int num)
{
int fact=1,i;
for(i=1;i<=num;i++)
{
fact=fact*i;
}
System.out.println("Factorial = "+fact);
}
}
package p2;
package p3;
import p1.*;
import p2.*;
f.factorial(8);
e.check(939);
}
}
d:\> cd packall
To Execute :
Interfaces :
The interfaces are used for defining the layout of the class . They contains
the data members and the methods prototypes.
interface interfacename
{
datatype1 item1=value1;
: :
: :
datatype1 itemn=valuen;
The dataitems of the interface are considered as final so , they should be intialised
at the time of the interface definition.
Note that the dataitems of the interface are considered as final so they should be
initialised at the
time of there decalaration
The method which are decalared in the interface are implemented or defined
in the classes which are created with the help of this interface .When they are
defined in these classes , they should be defined as public .
interface A
{
void m1();
}
class B implements A
{
public void m1()
{
class InterDemo
{
public static void main(String args[ ])
{
B ob=new B();
ob.m1();
}
}
So, when we create a class using the Interface we have to make use of the
implements keyword.
The method whose prototypes are given in the interface , should be defined as
public when defined in the class implemented by as interface.
interface StackLayout
{
void push(int val);
int pop();
}
class Stack implements StackLayout
{
int stk[ ];
int top;
Stack()
{
stk=new int[50];
top=-1;
}
stk[top]=val;
}
}
if(top==-1)
{
System.out.println("Stack Underflow");
val=-1;
}
else
{
val=stk[top];
top=top-1;
}
return val;
}
}
class StackDemo
{
public static void main(String args[ ])
{
Stack ob=new Stack();
ob.push(6);
ob.push(10);
}
}
interface A
{
void m1();
}
interface B extends A
{
void m2();
}
class C implements B
{
public void m1()
{
System.out.println("Method M1");
}
public void m2()
{
System.out.println("Method M2");
}
}
class InterDemo2
{
public static void main(String args[ ])
{
C ob=new C();
ob.m1();
ob.m2();
}
}
Multiple Inheritance :
interface A
{
void m1();
}
interface B
{
void m2();
}
interface C extends A,B
{
void m3();
}
class D implements C
{
public void m1()
{
System.out.println("Method M1");
}
public void m2()
{
System.out.println("Method M2");
}
public void m3()
{
System.out.println("Method M3");
}
}
class InterDemo3
{
public static void main(String args[ ])
{
D ob=new D();
ob.m1();
ob.m2();
ob.m3();
}
}