Packages PDF
Packages PDF
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86,
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86,
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 java.lang
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:-java.lang, java.io ,java.util…..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,
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86,
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
packagecom.sravya; import java.io.*;
import java.io.*; importjava.lang.*;
importjava.lang.*; packagecom.sravya;
Example-2:Invalid Example-4:Invalid
import java.io.*; packagecom.sravya;
packagecom.sravya; packagecom.tcs;
import java.io.*;
Java.io package:-The classes which are used to perform the input output operations that are present
in the java.io packages.
java
|------io
|--FileInputStream(class)
|--FileOutputStream(class)
|--FileReader(class)
|--FileWriter(class)
|--Serializable(interface)
Java.net package:-The classes which are required for connection establishment in the network that
classes are present in the java.net 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,
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
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.
packagecom.sravya.java.corejava;
class Test
{ public static void main(String[] args)
{ System.out.println("package first example");
}
}
class A
{}
class B
{}
interface It
{}
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86,
Step-4:-execution process.
Execute the .class file by using fully qualified name(class name with complete package structure)
javacom.sravya.java.corejava.Test
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 java.lang.*;
o Importing application required classes
Import java.lang.System;
Import java.lang.String;
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,
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: Sravya.java
package com.sravya.states.info;
public class Sravya
{ public void ts(){System.out.println("jai telengana");}
public void ap(){System.out.println("jai andhra");}
public void others(){System.out.println("jai jai others");}
}
File-2: Tcs.java
packagecom.tcs.states.requiredinfo;
import com.sravya.states.info.*;
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,
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()
{ System.out.println(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.
A.java:-
package app1;
public class A
{ protectedint fee=1000;
};
B.java:-
package app2;
import app1.*;
public class B extends A
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86,
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 :-
Test.java:-
package app1;
public class Test
{ public void m1(){System.out.println("app1.Test class m1()");}
}
A.java:-
package app1.corejava;
public class A
{ public void m1(){System.out.println("app1.corejava.A class m1()");}
}
Ratan.java:-
import app1.Test;
import app1.corejava.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,
Example :-
Test.java:-
package app1;
public class Test
{ public void m1(){System.out.println("app1.Test class m1()");}
}
X.java:-
package app1.corejava;
public class X
{ public void m1(){System.out.println("app1.core.X class m1()");}
}
Y.java:-
package app1.corejava.advjava;
public class Y
{ public void m1(){System.out.println("app1.corejava.advjava.Y class m1()");}
}
Z.java:-
Package app1.corejava.advjava.structs;
public class Z
{ public void m1(){System.out.println("app1.corejava.advjava.structs.Z class m1()");}
}
Ratan.java:-
import app1.Test;
import app1.corejava.X;
import app1.corejava.advjava.Y;
import app1.corejava.advjava.structs.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,
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 java.lang.System.*;
The above line is used to call all the static members of System class directly into application
without using class name.
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86,
Example:-
packagecom.dss.java.corejava;
public class Sravya
{ public static int fee=1000;
public static void course()
{ System.out.println("core java");
}
public static void duration()
{ System.out.println("1-month");
}
public static void trainer()
{ System.out.println("ratan");
}
};
without static import with static import
packagecom.tcs.course.coursedetails; packagecom.tcs.course.coursedetails;
importcom.dss.java.corejava.*; import static com.dss.java.corejava.Sravya.*;
classTcs classTcs
{ public static void main(String[] args) { public static void main(String[] args)
{ System.out.println(Sravya.fee); { System.out.println(fee);
Sravya.course(); course();
Sravya.duration(); duration();
Sravya.trainer(); trainer();
} }
} }
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86,
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
|--->A.class
|--->B.class
|--->C.class
|--->ratan
|--->D.class
In above example when we import com.sravya.*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 com.sravya.ratan.*).
File-1: A.java
packagejav.corejava;
public class A Package structure:-
{ public void m1() jav
{System.out.println("core java World!"); |-->corejava
} |--->A.class
}
File-2: B.java:
packagejav.corejava.advjava; jav
public class B |-->corejava
{ public void m1() |--->A.class
{System.out.println("Adv java World!"); |--->advjava
} |--->B.class
}
File-3: C.java:-
packagejav.corejava.advjava.structs; }
public class C File-4:- MainTest.java
{ public void m1() Package structure :-
{System.out.println("Structs World!"); jav
} |-->corejava
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86,
|--->A.class |--->structs
|--->advjava |--->C.class
|--->B.class
importjav.corejava.A;
importjav.corejava.advjava.B;
importjav.corejava.advjava.structs.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.
packagejava.lang;
class Test
{ public static void main(String[] args)
{ System.out.println("Ratan World!");
}
}
class A
{ };
D:\DP>javac -d . Test.java
D:\DP>java java.lang.Test
Exception in thread "main" java.lang.SecurityException: Prohibited package name: java.lang
3) Protected
4) default
File-2:-
Package com.dss;
Import com.dss.st.Test;
class Test1
{ public static void main(String[] args)
{ //Test t = new Test(); compilation error Test() has private access in Test
Test t1 = Test.getInstance();
Test t2 = Test.getInstance();
System.out.println(t1.hashCode());//31168322
System.out.println(t2.hashCode());//31168322
}
};
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86, 80 96
96 96 96, 9246212143 | www.durgasoft.com 18 | 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 | www.durgasoft.com 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 | www.durgasoft.com 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 | www.durgasoft.com 21 | P a g e