AMITY INSTITUTE OF INFORMATION
TECHNOLOGY
BCA B C&D 247
Semester-III
Object Oriented Programming Using
Java
Prof(Dr)Laxmi Ahuja
Learning Objectives
•Imparting java programming skill to students
•Knowledge of object oriented paradigm in
context of Java programming language
•Designing desktop applications using latest
Java based API
Contents to be Covered
• Introduction to Packages
• User Defined Packages
• JAVA Packages
PACKAGES
• A package can be defined as a grouping of related types(classes,interface
etc)providing access protection and name space management.
• Package defines a boundary to see how classes and interfaces interact with
one another. It also acts as a mode of protection..
• Java language programs automatically import all classes in [Link]
package.
• Packages help us to group similar classes and helps us to avoid conflict in
naming classes.
• Classes ,methods and variables can be protected in a better way.
How to create custom packages and how to encapsulate classes and
methods in that package
As packages consist of classes and Interfaces
Example
[Link]—scanner class—nextInt() nextFloat()—(Methods)
[Link].*;--BufferedReader() --read()—readLine()
import [Link].*; //All public classes are available
We can create these types of classes and we can place in User defined
Package
import [Link]; //Only class having the name [Link] is
available for use.
package pack;
public class packdemo
{
public void show()
{
[Link]("Welcome to java");
}
}
Save it as [Link]
Compile it javac –d . [Link] //-d . Create pack folder in current
directory
//don’t run it
import [Link];
class pack1
{
public static void main(String args[])
{
packdemo pc=new packdemo();
[Link]();
}
}
Save it with [Link]
Javac [Link]
Java pack1
User-Defined Package
• Package Keyword is used followed by package_name;
• Package not contain main class
• Multiple programs should be written for placing multiple classes in same package
Example:
//For creation of a package
package pack1;
public class A //public accesspecifier is must
{
public void show()
{
[Link]("A clas");
}}
//save [Link]
//compile ---javac –d . [Link]
//-d. Is a current directory
-d create director of pack where we have [Link] file
//no need to execute
//used to import a user defined package
import pack1.A;
class maindemo
{
public static void main(String args[])
{
A obj1=new A();
[Link]();
}
}
//save [Link]//actual prog where we are writing main program
//javac [Link] //compilation is simple
Java MainDemo
package pack1;
public class class1
{
public int a=20;
[Link]("Main of class one");
}}
Save as [Link]
Compile only
Javac –d . [Link]
import pack1.class1;
public class class2
{
public static void main(String args[])
{
int b=30;
class1 c1=new class1();
[Link]("b=" +b);
[Link]("a=" +c1.a);
}}
//save as [Link]
Javac [Link]
Java class2
How to create package
First statement in a java program should be a package
statement.
package [Link];
To import classes from a package , import command is
used.
package pack;
public class class1
{
public int a=20;
public static void main(String args[])
{
[Link]("Main of class one");
}}
• save it in a folder pack
• To compile : c:\cd pack
• C:\pack>javac [Link]
• C:\pack>cd..
• C:\java pack .class1
package pack1;
import pack.*;
public class class2
{
public static void main(String args[])
{
int b=30;
class1 c1=new class1();
[Link]("b=" +b);
[Link]("a=" +c1.a);
}
}
• save it in a pack1
• c:\cd pack1
• c:\pack1>set classpath= c:\ ;.
• c:\pack1>javac [Link]
• cd..
• c:\>java pack1.class2
• CLASSPATH variable plays a significant role in
locating classes. If CLASSPATH is not set java will
look for the classes in the current directory and the
default directory is generally c:\jdk1.6\lib
• If CLASSPATH is specified then java will look only
in those directories specified by the variable
CLASSPATH
package pack;
public class fact1
{
public void fact(int n)
{
int f=1;
for(int i=0;i<n;i++)
{
f*=i;
}
[Link]("Factorial of" +n+ "is" +f);
}
}
Save as [Link]
javac –d . [Link]
import pack.fact1;
class factmain
{
public static void main(String args[])
{
fact1 f1=new fact1();
[Link](5);
}
}
Save as [Link]
Javac –d . [Link]
THANK YOU