0% found this document useful (0 votes)
409 views21 pages

Java Packages Explained by Durga Soft

The document discusses Java packages. It explains that Java packages organize Java classes and interfaces into logical groups and correspond to directories in the file system. The key predefined packages include java.lang, java.io, and java.net, which contain commonly used classes like String, FileInputStream, and Socket. User-defined packages can also be created, and package names should follow conventions related to domain names and project structure. Packages improve code organization, reusability, and maintenance of large Java projects.

Uploaded by

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

Java Packages Explained by Durga Soft

The document discusses Java packages. It explains that Java packages organize Java classes and interfaces into logical groups and correspond to directories in the file system. The key predefined packages include java.lang, java.io, and java.net, which contain commonly used classes like String, FileInputStream, and Socket. User-defined packages can also be created, and package names should follow conventions related to domain names and project structure. Packages improve code organization, reusability, and maintenance of large Java projects.

Uploaded by

Hari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

JAVA Means DURGA SOFT

nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038,  040 – 64 51 27 86,

80 96 96 96 96, 9246212143 | [Link] Page 1


JAVA Means DURGA SOFT

nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038,  040 – 64 51 27 86,

80 96 96 96 96, 9246212143 | [Link] Page 2


JAVA Means DURGA SOFT

Packages
java-language:-
in java James Gosling is maintained predefined support in the form of packages and
these packages contains classes & interfaces, and these classes and interfaces contains
predefined methods & variables.
java-language james gosling

packages [Link]

classes & interfaces System,String……..

methods& variables length(),charAt(),concat()…

java source code:-


 java is a open source software we are able to download it free of cost and we are able
to see the source code of the java.
 The source code location C:\Program Files\Java\jdk1.7.0_75\src(zip file) extract the zip file.
 Java contains 14 predefined packages but the default package in java if [Link].
package.
[Link] [Link] [Link] [Link]
[Link] [Link] [Link] [Link]
[Link] [Link] [Link]
[Link] [Link] [Link]
Note : The default package in java is [Link] package.
Note : package is nothing but physical folder structure.

Types of packages:-
There are two types of packages in java
1) Predefined packages.
2) User defined packages.
Predefined packages:
The predefined packages are introduced by James Gosling and these packages contains
predefined classes & interfaces and these class & interfaces contains predefined variables and methods.
Example:-[Link], [Link] ,[Link]…..etc
User defined packages:-
 The packages which are defined by user, and these packages contains user defined classes and
interfaces.
 Declare the package by using package keyword.
syntax : package package-name;

nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038,  040 – 64 51 27 86,

80 96 96 96 96, 9246212143 | [Link] Page 3


JAVA Means DURGA SOFT

example : package [Link];

nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038,  040 – 64 51 27 86,

80 96 96 96 96, 9246212143 | [Link] Page 4


JAVA Means DURGA SOFT

 Inside the source file it is possible to declare only one package statement and that statement
must be first statement of the source file.
Example-1:valid Example-3:Invalid
[Link]; import [Link].*;
import [Link].*; [Link].*;
[Link].*; [Link];

Example-2:Invalid Example-4:Invalid
import [Link].*; [Link];
[Link]; [Link];
import [Link].*;

some predefined package and it’s classes & interfaces:-


[Link]:-The most commonly required classes and interfaces to write a sample program is
encapsulated into a separate package is called [Link] package.
java
|------lang
|--String(class)
|--StringBuffer(class)
|--Object(class)
|--Runnable(interface)
|--Cloneable(interface)
Note:- the default package in the java programming is [Link] package.

[Link] package:-The classes which are used to perform the input output operations that are present
in the [Link] packages.
java
|------io
|--FileInputStream(class)
|--FileOutputStream(class)
|--FileReader(class)
|--FileWriter(class)
|--Serializable(interface)

[Link] package:-The classes which are required for connection establishment in the network that
classes are present in the [Link] package.
java
|------net
|--Socket(class)
|--ServerSocket(class)
|--URL(class)
|--SocketOption(interface)

nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038,  040 – 64 51 27 86,

80 96 96 96 96, 9246212143 | [Link] Page 5


JAVA Means DURGA SOFT

Package name coding conventions :-(not mandatory but we have to fallow)


1) The package name must reflect with organization domain name(reverse of domain name).
Domain name:- [Link]
Package name:- Package [Link];
2) Package name must reflect with project name.
Project name :- bank
package :- Package [Link];
3) The project name must reflect with project module name.
Domain name:- [Link]
Project name:- bank
Module name:- deposit
package name:- Package [Link];
Package [Link];

Reverse of Project Module


keyword domain name name name

Advantages of packages:-
company name : tcs
project name : bank

module-1Deposit module-2withdraw
com com
|-->tcs |-->tcs
|-->bank |-->bank
|-->deposit |-->withdraw
|--->.class files |--->.class files

Module-3moneytranfer module-4accountinfo
com com
|-->tcs |-->tcs
|-->bank |-->bank
|-->moneytranfer |-->accountinfo
|--->.class files |--->.class files

1) It improves parallel development of the project.


2) Project maintenance will become easy.
3) It improves sharability of the project.
4) It improves readability.
5) It improves understandability.
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038,  040 – 64 51 27 86,

80 96 96 96 96, 9246212143 | [Link] Page 6


JAVA Means DURGA SOFT

Note :- In real time the project is divided into number of modules that each and every
module is nothing but package statement.

Example-1:-
Step-1: write the application with package statement.
[Link];
class Test
{ public static void main(String[] args)
{ [Link]("package first example");
}
}
class A
{}
class B
{}
interface It
{}

Step-2: compilation process


If the source file contains the package statement then compile that application by using
fallowing command.
D:\>javac -d . [Link]

Place the directory Java source


Creates folder
Java structure in current file name
structure
compiler working folder

nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038,  040 – 64 51 27 86,

80 96 96 96 96, 9246212143 | [Link] Page 7


JAVA Means DURGA SOFT

Step-3:- folder Structure.


com
|-->sravya
|-->java
|-->corejava
|-->[Link]
|-->[Link]
|-->[Link]
|-->[Link]

Step-4:-execution process.
Execute the .class file by using fully qualified name(class name with complete package structure)
[Link]
output : package first example

Example-2:-
Error-1 :-
 If it is a predefined package or user defined package Whenever we are using other package
classes then must import that package by using import statement.
 If the application required two classes (System,String) then We are able to import the classes in
two ways
o Importing all classes.
Import [Link].*;
o Importing application required classes
Import [Link];
Import [Link];
In above two approaches second approach is recommended because it is importing application
required classes.

Error-2:-
 Whenever we are using other package classes then that classes must be public otherwise
compiler generate error message.
Error:class is not public we are unable to access outside package.
Public modifier:-
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038,  040 – 64 51 27 86,

80 96 96 96 96, 9246212143 | [Link] Page 8


JAVA Means DURGA SOFT

 Public modifier is applicable for variables,methods,classes.


 All packages are able to access public members.
Default modifier:-
 It is applicable for variables,methods,classes.
 We are able to access default members only within the package and it is not possible to access
outside package .
 Default access is also known as package level access.
 The default modifier in java is default.

Error-3:-
 Whenever we are using other package class member that members also must be public.
Note : When we declare class as public the corresponding members are not public,
if we want access public class members that members also must be public.

File-1: [Link]
package [Link];
public class Sravya
{ public void ts(){[Link]("jai telengana");}
public void ap(){[Link]("jai andhra");}
public void others(){[Link]("jai jai others");}
}

File-2: [Link]
[Link];
import [Link].*;
class Tcs
{ public static void main(String[] args)
{ Sravya s = new Sravya();
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038,  040 – 64 51 27 86,

80 96 96 96 96, 9246212143 | [Link] Page 9


JAVA Means DURGA SOFT

[Link](); [Link](); [Link]();


}
}
E:\>javac -d . [Link] compilation of Sravya
E:\>javac -d . [Link] compilation of Tcs
E:\>java [Link] execution of Tcs
jaitelengana
jaiandhra
jaijai others

Example:-
Private modifier:-
 privatemodifier applicable for methods and variables.
 We are able to access private members only within the class and it is not possible to access even
in child classes.
class Parent
{ privateint a=10;
};
class Child extends Parent
{ void m1()
{ [Link](a); //a variables is private Child class unable to access
}
public static void main(String[] arghs)
{ Child c = new Child();
c.m1();
}
};
error: a has private access in Parent
Note :- the most accessable modifier in java Is public & most restricted modifier in java is private.

Example :-
Protected modifier:-
 Protected modifier is applicable for variables,methods.
 We are able access protected members with in the package and it is possible to access outside
packages also but only in child classes.
 But in outside package we can access protected members only by using child reference. If wetry
to use parent reference we will get compile time error.
[Link]:-
package app1;
public class A
{ protectedint fee=1000;
};
[Link]:-
package app2;
import app1.*;
public class B extends A
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038,  040 – 64 51 27 86,

80 96 96 96 96, 9246212143 | [Link] Page 10


JAVA Means DURGA SOFT

{ public static void main(String[] args)


{ B b = new B( );
[Link]([Link]);
}
};
Parent-class method child-class method
Default default -->valid (same level)
protected , public --> valid (increasing permission )
Private --> invalid (decreasing permission)

Public public --> valid (same level)


Default,private,protected -->invalid(decreasing permission)

Protected protected --->valid (same level)


Public --->valid (increasing permission)
Default,private ---->invalid (decreasing permission)

Case 1:- same level [default-default]


abstract class Test
{ abstract void m1(); // default modifier
};
class Test1 extends Test
{ void m1(){[Link]("m1 method");} //default modifier
}
Case 2:- increasing permission [protected-public]
abstract class Test
{ protected abstract void m1(); // protected modifier access
};
class Test1 extends Test
{ public void m1(){[Link]("m1 method");} //public modifier access
};
Case3 :- decreasing permission [public-protected]
abstract class Test
{ public abstract void m1(); // public modifier
};
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038,  040 – 64 51 27 86,

80 96 96 96 96, 9246212143 | [Link] Page 11


JAVA Means DURGA SOFT

class Test1 extends Test


{ protected void m1(){[Link]("m1 method");} //protected modifier
};

Summary of variables:-
modifier Private no-modifier protected public
Same class yes yes yes yes
Same package sub class no yes yes yes
Same package non sub class no yes yes yes
Different package sub class no no yes yes
Different package non sub class no no no yes

Example :-
[Link]:-
package app1;
public class Test
{ public void m1(){[Link]("[Link] class m1()");}
}
[Link]:-
package [Link];
public class A
{ public void m1(){[Link]("[Link].A class m1()");}
}
[Link]:-
import [Link];
import [Link].A;
classRatan
{ public static void main(String[] args)
{ Test t = new Test();
t.m1();
A a =new A();
a.m1();
}
}

nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038,  040 – 64 51 27 86,

80 96 96 96 96, 9246212143 | [Link] Page 12


JAVA Means DURGA SOFT

Example :-
[Link]:-
package app1;
public class Test
{ public void m1(){[Link]("[Link] class m1()");}
}
[Link]:-
package [Link];
public class X
{ public void m1(){[Link]("[Link].X class m1()");}
}
[Link]:-
package [Link];
public class Y
{ public void m1(){[Link]("[Link].Y class m1()");}
}
[Link]:-
Package [Link];
public class Z
{ public void m1(){[Link]("[Link].Z class m1()");}
}
[Link]:-
import [Link];
import [Link].X;
import [Link].Y;
import [Link].Z;
classRatan
{ public static void main(String[] args)
{ Test t = new Test(); t.m1();
X x = new X(); x.m1();
Y y = new Y(); y.m1();
Z z = new Z(); z.m1();
}
};
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038,  040 – 64 51 27 86,

80 96 96 96 96, 9246212143 | [Link] Page 13


JAVA Means DURGA SOFT

Note :- applicable modifiers on constructors


1) Public
2) Private
3) Default (if we are not providing modifiers)
4) Protected
Private constructors:-
class Parent
{ private Parent(){}//private constructor
}
class Child extends Parent
{ Child()
{super();} //we are calling parent class private constructor it is not possible
};
D:\>javac [Link]
[Link][Link] Parent() has private access in Parent

Static import:-
1. This concept is introduced in 1.5 version.
2. if we are using the static import it is possible to call static variables and static methods of a
particular class directly to the application without using class name.
a. import static [Link].*;
The above line is used to call all the static members of System class directly into application
without using class name.

Ex:-without static mport


[Link].*; Ex :- with static import
class Test import static [Link].*;
{ public static void main(String[] args) class Test
{ { public static void main(String[] args)
[Link]("Hello World!"); {
[Link]("Hello World!"); [Link]("ratan world");
[Link]("Hello World!"); [Link]("ratan world");
} [Link]("ratan world");
} }
};

nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038,  040 – 64 51 27 86,

80 96 96 96 96, 9246212143 | [Link] Page 14


JAVA Means DURGA SOFT

Example:-
[Link];
public class Sravya
{ public static int fee=1000;
public static void course()
{ [Link]("core java");
}
public static void duration()
{ [Link]("1-month");
}
public static void trainer()
{ [Link]("ratan");
}
};
without static import with static import
[Link]; [Link];
[Link].*; import static [Link].*;
classTcs classTcs
{ public static void main(String[] args) { public static void main(String[] args)
{ [Link]([Link]); { [Link](fee);
[Link](); course();
[Link](); duration();
[Link](); trainer();
} }
} }

nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038,  040 – 64 51 27 86,

80 96 96 96 96, 9246212143 | [Link] Page 15


JAVA Means DURGA SOFT

Example :-
 When you import main package we are able to access only package classes, it is not possible to
access sub package classes, if we want sub package classes must import sub packages also.
Ex:-
com
|-->sravya
|--->[Link]
|--->[Link]
|--->[Link]
|--->ratan
|--->[Link]
In above example when we import [Link].*it is possible to access only three classes(A,B,C) but it is
not possible to access sub package classes (ratan package D class)if we want sub package classes must
import sub package(import [Link].*).

File-1: [Link]
[Link];
public class A Package structure:-
{ public void m1() jav
{[Link]("core java World!"); |-->corejava
} |--->[Link]
}
File-2: [Link]:
[Link]; jav
public class B |-->corejava
{ public void m1() |--->[Link]
{[Link]("Adv java World!"); |--->advjava
} |--->[Link]
}
File-3: [Link]:-
[Link]; }
public class C File-4:- [Link]
{ public void m1() Package structure :-
{[Link]("Structs World!"); jav
} |-->corejava
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038,  040 – 64 51 27 86,

80 96 96 96 96, 9246212143 | [Link] Page 16


JAVA Means DURGA SOFT

|--->[Link] |--->structs
|--->advjava |--->[Link]
|--->[Link]
[Link].A;
[Link].B;
[Link].C;
classMainTest
{ public static void main(String[] args)
{ A a = new A(); a.m1();
B b = new B(); b.m1();
C c = new C(); c.m1();
}
}

Example :- in java it is not possible to use predefined package names as a user defined packages.
[Link];
class Test
{ public static void main(String[] args)
{ [Link]("Ratan World!");
}
}
class A
{ };
D:\DP>javac -d . [Link]
D:\DP>java [Link]
Exception in thread "main" [Link]: Prohibited package name: [Link]

Applicable modifiers on constructors:-


1) Public
2) Private
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038,  040 – 64 51 27 86,

80 96 96 96 96, 9246212143 | [Link] Page 17


JAVA Means Durgasoft

3) Protected
4) default

we are achieving singleton class creation by using private constructors in java:-


 when we declare constructor with private modifier we can’t create object outside of the class.
 Singleton class allows to create only one object of particular class and we are achieving singleton
class creation by using private constructors.
 In some scenarios it is appropriate to have exactly one instance of class like,
o Window manager
o File systems
o Project manager
o Admin
These type of objects are called singleton objects.
file-1:-
package [Link];
class Test
{ public static Test t=null;
private Test(){}
public static Test getInstance()
{ if (t==null)
{ t = new Test();
}
return t;
}
public void disp()
{ [Link]("this is ratan singleton class");
}
};

File-2:-
Package [Link];
Import [Link];
class Test1
{ public static void main(String[] args)
{ //Test t = new Test(); compilation error Test() has private access in Test
Test t1 = [Link]();
Test t2 = [Link]();
[Link]([Link]());//31168322
[Link]([Link]());//31168322
}
};

nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038,  040 – 64 51 27 86, 80 96
96 96 96, 9246212143 | [Link] 18 | P a g e
JAVA Means Durgasoft

Source file Declaration rules:-


The source file contains the fallowing elements
1) Package declaration---optional-----at most one package(0 or 1)--1st statement
2) Import declaration-----optional-----any number of imports--------2nd statement
3) Class declaration--------optional-----any number of classes---------3rd statement
4) Interface declaration---optional----any number of interfaces-----3rd statement
5) Comments declaration-optional----any number of comments----3rd statement
a. The package must be the first statement of the source file and it is possible to declare at most
one package within the source file .
b. The import session must be in between the package and class statement. And it is possible to
declare any number of import statements within the source file.
c. The class session is must be after package and import statement and it is possible to declare any
number of class within the source file.
i. It is possible to declare at most one public class.
ii. It is possible to declare any number of non-public classes.
d. The package and import statements are applicable for all the classes present in the source file.
e. It is possible to declare comments at beginning and ending of any line of declaration it is
possible to declare any number of comments within the source file.
Preparation of user defined API (application programming interface document):-
1. API document nothing but user guide.
2. Whenever we are buying any product the manufacturing people provides one document called
user guide. By using user guide we are able to use the product properly.
3. James gosling is developed java product whenever james gosling is delivered the project that
person is providing one user document called API(application programming interface)
document it contains the information about how to use the product.
4. To prepare user defined apidocument for the user defined projects we must provide the
description by using documentation comments that information is visible in API document.

nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038,  040 – 64 51 27 86, 80 96
96 96 96, 9246212143 | [Link] 19 | P a g e
JAVA Means Durgasoft

nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038,  040 – 64 51 27 86, 80 96
96 96 96, 9246212143 | [Link] 20 | P a g e
JAVA Means Durgasoft

nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038,  040 – 64 51 27 86, 80 96
96 96 96, 9246212143 | [Link] 21 | P a g e

You might also like