0% found this document useful (0 votes)
5 views15 pages

Packages Applet

The document provides an overview of Java packages, including pre-defined and user-defined packages, along with examples of how to create and use them. It also covers interfaces, their implementation, and inheritance in interfaces, demonstrating multiple inheritance with examples. The document includes step-by-step instructions for compiling and executing Java programs that utilize these concepts.

Uploaded by

Pace Infotech
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views15 pages

Packages Applet

The document provides an overview of Java packages, including pre-defined and user-defined packages, along with examples of how to create and use them. It also covers interfaces, their implementation, and inheritance in interfaces, demonstrating multiple inheritance with examples. The document includes step-by-step instructions for compiling and executing Java programs that utilize these concepts.

Uploaded by

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

Packages : Packages is a collection of classes .

In java , there are two types of


packages :

1. pre-defined packages

2. user defined packages

1. Pre-defined packages : These packages comes with the java library . Some of the
packages are listed below :
Packages Description

(i) java.lang This package is automatically imported .It contains


the java classes which we normally use e.g.
String,Integer,etc.

(ii) java.applet This package will contain the classes related


to the applet programming .

(iii) java.awt This package contains the classes which will be


helpful in creating the GUI components .

(iv) java.awt.event This package contains interfaces and classes for


implementing and handling the events

(v) java.io This package contains the classes related to


reading data from keyboard , and handling
the stream. And also interacting with the files

(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.........

2. User Defined packages :

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;

Consider the following program ,

package p;

class Sample
{
int i,j;

public void setij(int a,int b)


{

i=a;

j=b;
}

public void show()


{
System.out.println("i="+i);

System.out.println("j="+j);
}
public static void main(String args[ ])
{

Sample ob=new Sample();

ob.setij(6,7);

ob.show();
}
}

Step for executing the above program :

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

3. Now, make the folder d:\p as your current directory.

Move to dos ,

run----> type command.com

The following prompt will appaer.

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

Multiple classes in the same package :

In this case a single package contians the multiple classes .

We have created a folder with name packall in the drive d:\ and in this we have to
save all the .java files.

Now type the following code in the file , Factorial.java

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);
}
}

And , now type the following code in the file ,EvenOdd.java

package packall;
class EvenOdd
{
void check(int num)
{
if(num%2==0)
System.out.println("Even.");
else
System.out.println("Odd.");
}
}

Now, type the following code and save it in , All.java

package packall;

class All
{
public static void main(String args[ ])
{
int num;

num=Integer.parseInt(args[0]);

Factorial f=new Factorial();

f.factorial(num);

EvenOdd e=new EvenOdd();

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 .

The general form of the import statement is ,

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

Steps for package p1 :

i) Create the directory packall in the drive d:

ii) Create the directory p1 in d:\packall

iii) Type the following code in the file Factorial.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);
}
}

iv) Now save the file in the directory d:\packall\p1.

Steps for package p2 :

i) Create the directory p2 in d:\packall

iii) Type the following code in the file EvenOdd.java

package p2;

public class EvenOdd


{
public void check(int num)
{
if(num%2==0)
System.out.println("Even");
else
System.out.println("Odd");
}
}

iv) Now save the file in the directory d:\packall\p2.

Steps for package p3 :

i) Create the directory p3 in d:\packall

ii) Type the following code in the file UseAll.java

package p3;

import p1.*;

import p2.*;

public class UseAll


{
public static void main(String args[ ])
{
Factorial f=new Factorial();

f.factorial(8);

EvenOdd e=new EvenOdd();

e.check(939);

}
}

iv) Now save the file in the directory d:\packall\p3.


To compile :

d:\> cd packall

d:\packall> javac p1\*.java

d:\packall> javac p2\*.java

d:\packall> javac p3\*.java

To Execute :

d:\packall> java p3.UseAll

Interfaces :

The interfaces are used for defining the layout of the class . They contains
the data members and the methods prototypes.

The general form of the interface is ,

interface interfacename
{
datatype1 item1=value1;
: :
: :
datatype1 itemn=valuen;

returntype1 methodname1(argument list1);


: :
: :
returntypen methodnamen(argument listn);
}

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 .

CONSIDER THE FOLLOWING PROGRAM ,

interface A
{
void m1();
}

class B implements A
{
public void m1()
{

System.out.println("B class implementation");


}
}

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;
}

public void push(int val)


{
if(top==50)
System.out.println("Stack Overflow");
else
{
top=top+1;

stk[top]=val;
}
}

public int pop()


{
int 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);

System.out.println("Data = " + ob.pop());

System.out.println("Data = " + ob.pop());

}
}

Using Inheritance in the Interfaces :

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();
}
}

You might also like