You are on page 1of 30

Object Oriented Programming Methodology

Using Java

Prof: Pradnya Sadigale


(E&TC Department)
SOET- LOhegaon
Prof.Pradnya Sadigale
PACKAGES
Packages
• A java package is a group of similar types of classes, interfaces
and sub-packages.
• Package in java can be categorized in two form, built-in package
and user-defined package.
• There are many built-in packages such as java, lang, awt, javax,
swing, net, io, util, sql etc.
• Here, we will have the detailed learning of creating and using
user-defined packages.
Advantage of Java Package
1) Java package is used to categorize the classes and interfaces so
that they can be easily maintained.
2) Java package provides access protection.
3) Java package removes naming collision.
Types of package in java
1.built-in packages :These are predefined packages
in java. They contain wide range of classes and
methods to perform various Functions(e.g java.io
,java.lang,java.math ,java.util etc).

2.User Define Packages: These are created by the user.


The user can group user defined classes in user
defined packages.
Built in package
Packages are just container that hold things In java
packages do exactly that expect they are container
for a classes .
Why using Packages
Avoid Naming conflicts by giving full-name
Creating a package
Step 1:
package test; //package package_name
Step 2:
pacakge test
class mumbai
{
//member 1
//member 2
}
Creating a package
Step 3: store program in folder with the same name as
that package (india).
1.Store the program in a folder name test.
2.Store the program file as name of class containing the
main() method .
3.Store the program as mumbai.java
Step 4:
Compile the program in the package folder using javac
Enter into the folder india via command prompt using cd
india and complie using
javac mumbai.java
Creating a package
Step 5: exit package folder and execute the program
using: java package_name.class_name
Thus above example
1) Exit package folder using cd
2) Execute the program using java india.mumbai
Creating a package
Package india;
Class mumbai
{
P s v m(String args[])
{
SOPln;͞mumbai , the city of dreams!͟Ϳ;
}}
//output
C:>cd india
C:>india>javac mumbai.java
C:>india>cd..
C:>java india.mumbai
Mumbai, the city of dream!
Creating a sub package
Sub package is similar to creating a package
The folder of sub-package must be store inside the
package.
For example
Package india contains a class mumbai and sub-class Goa
Sub package goa contains class panaji

India goa
Class mumbai class punaji
package india.goa;
class panaji
{
public static void main(String args[])
{
System.out.println;͞panaji goas largest city");
} }
//output
C:>cd india
C:>india>cd goa
C:>india>goa>javac panaji.java
C:>india>goa>cd..
C:>india>java india.goa.panaji
panaji goas largest city
Access specifies
Package help to protect your classes from
outside code using access specifies
->public
->protected
->private
->default
Example of packages
package world;
class Hello
{ Z:\a120>cd world
public static void main(String args[]) Z:\a120>world>javac Hello.java
Z:\a120>world>cd..
{ Z:\a120>java world.Hello
System.out.println("Hello Friends"); Hello Friends
}
}
class Box
//Importing a package // constructor used when cube is created
{ private double width; Box(double len)
Write a program to create
private double height; a package
{
,import itprivate
in another program andwidth
double depth; call = import
height = depth = len;
world.india.*;
// construct clone of an
the method in the packageobject }
class Hello1
Box(Box ob) // compute and return volume
{ {
double volume()
package world.india;
width = ob.width; { public static void
height = ob.height;
public class Hello main(String
return width * height args[])
* depth;
depth = ob.depth; } {
{} } Hello h=new Hello();
Box(doublepublic void
w, double display()
h, double d) h.display();
{ { class B oxWeight extends
} Box
width = w; {
System.out.println("Hello Indians"); }
height = h; double weight; // weight of box
depth = d;}
}} BoxWeight(BoxWeight ob) {
Box() // passobject to constructor
{ super(ob);
width = -1; // use -1 to indicate weight = ob.weight;
height = -1; // an uninitialized }
depth = -1; // box
}
package package1; import package1.*;
public class Test1 public class PackageDemo
{ {
int a; public static void main(String args[])
public Test1() {
{ Test1 x=new Test1();
a=1; System.out.println("Sum="+x.add(25));
} }
public int add(int b) }
{
return(a+b); /*****output*******
} Z:\a120>cd package1
} Z:\a120>package1>javac Test1.java
Z:\a120>package1>cd..
Z:\a120>javac PackageDemo.java
Z:\a120>java PackageDemo
Sum=26
*/

You might also like