0% found this document useful (0 votes)
3 views

Java Packages

The document outlines the learning objectives and content for a course on Object Oriented Programming using Java, focusing on Java packages and user-defined packages. It explains the concept of packages in Java, how to create and use them, and provides examples of package creation and class encapsulation. Additionally, it discusses the importance of the CLASSPATH variable in locating Java classes.

Uploaded by

Ashly Sunil
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Java Packages

The document outlines the learning objectives and content for a course on Object Oriented Programming using Java, focusing on Java packages and user-defined packages. It explains the concept of packages in Java, how to create and use them, and provides examples of package creation and class encapsulation. Additionally, it discusses the importance of the CLASSPATH variable in locating Java classes.

Uploaded by

Ashly Sunil
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

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 java.lang


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
java.util—scanner class—nextInt() nextFloat()—(Methods)
java.io.*;--BufferedReader() --read()—readLine()

import java.io.*; //All public classes are available

We can create these types of classes and we can place in User defined
Package

import pack.subpack.MyClass; //Only class having the name pack.subpack is


available for use.
package pack;
public class packdemo
{
public void show()
{
System.out.println("Welcome to java");
}
}
Save it as packdemo.java

Compile it javac –d . packdemo.java //-d . Create pack folder in current


directory
//don’t run it
import pack.packdemo;
class pack1
{
public static void main(String args[])
{
packdemo pc=new packdemo();
pc.show();
}
}

Save it with pack1.java


Javac pack1.java
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()
{
System.out.println("A clas");
}}
//save A.java

//compile ---javac –d . A.java


//-d. Is a current directory
-d create director of pack where we have A.class 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();
obj1.show();
}
}
//save maindemo.java//actual prog where we are writing main program
//javac maindemo.java //compilation is simple

Java MainDemo
package pack1;
public class class1
{
public int a=20;
System.out.println("Main of class one");
}}

Save as class1.java
Compile only
Javac –d . Class1.java
import pack1.class1;
public class class2
{
public static void main(String args[])
{
int b=30;
class1 c1=new class1();
System.out.println("b=" +b);
System.out.println("a=" +c1.a);
}}
//save as class2.java
Javac class2.java
Java class2
How to create package
First statement in a java program should be a package
statement.

package pack.subpack;
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[])
{
System.out.println("Main of class one");
}}
• save it in a folder pack

• To compile : c:\cd pack

• C:\pack>javac class1.java

• 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();
System.out.println("b=" +b);
System.out.println("a=" +c1.a);
}
}
• save it in a pack1

• c:\cd pack1

• c:\pack1>set classpath= c:\ ;.

• c:\pack1>javac class2.java

• 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;
}
System.out.println("Factorial of" +n+ "is" +f);
}
}
Save as fact1.java
javac –d . fact1.java
import pack.fact1;
class factmain
{
public static void main(String args[])
{
fact1 f1=new fact1();
f1.fact(5);
}
}
Save as factmain.java
Javac –d . factmain.java
THANK YOU

You might also like