0% found this document useful (0 votes)
5 views15 pages

Package

The document provides an overview of Java packages, interfaces, and classes, including how to compile Java files and manage directory structures. It explains the importance of package naming conventions, the use of import statements, and the implementation of interfaces in classes. Additionally, it illustrates examples of creating and using classes and interfaces, along with the necessary commands for compilation and execution.

Uploaded by

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

Package

The document provides an overview of Java packages, interfaces, and classes, including how to compile Java files and manage directory structures. It explains the importance of package naming conventions, the use of import statements, and the implementation of interfaces in classes. Additionally, it illustrates examples of creating and using classes and interfaces, along with the necessary commands for compilation and execution.

Uploaded by

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

javac -d Destination_folder file_name.

java

avoid any conflicts with the names of classes and interfaces.

Following package example contains interface named animals −

/* File name : [Link] */

package animals;

interface Animal {

public void eat();

public void travel();

Now, let us implement the above interface in the same package animals −

package animals;

/* File name : [Link] */

public class MammalInt implements Animal {

public void eat() {

[Link]("Mammal eats");

public void travel() {

[Link]("Mammal travels");

public int noOfLegs() {

return 0;

public static void main(String args[]) {


MammalInt m = new MammalInt();

[Link]();

[Link]();

Now compile the java files as shown below −

$ javac -d . [Link]

$ javac -d . [Link]

Importing Java Package

If a class wants to use another class in the same package, the package name need not be used. Classes in
the same package find each other without any special syntax.

Example

Here, a class named Boss is added to the payroll package that already contains Employee. The Boss can
then refer to the Employee class without using the payroll prefix, as demonstrated by the following Boss
class.

package payroll;

public class Boss {

public void payEmployee(Employee e) {

[Link]();

What happens if the Employee class is not in the payroll package? The Boss class must then use one of
the following techniques for referring to a class in a different package.

● The fully qualified name of the class can be used. For example −

[Link]

● The package can be imported using the import keyword and the wild card (*). For example −

import payroll.*;

● The class itself can be imported using the import keyword. For example −
import [Link];

Example

package payroll;

public class Employee {

public void mailCheck() {

[Link]("Pay received.");

Example

package payroll;

import [Link];

public class Boss {

public void payEmployee(Employee e) {

[Link]();

public static void main(String[] args) {

Boss boss = new Boss();

Employee e = new Employee();

[Link](e);

Output

Pay received.

Note − A class file can contain any number of import statements. The import statements must appear
after the package statement and before the class declaration.
Directory Structure of a Java Package

Two major results occur when a class is placed in a package −

● The name of the package becomes a part of the name of the class, as we just discussed in the
previous section.

● The name of the package must match the directory structure where the corresponding bytecode
resides.

Here is simple way of managing your files in Java −

Put the source code for a class, interface, enumeration, or annotation type in a text file whose name is
the simple name of the type and whose extension is .java.

For example −

// File Name : [Link]

package vehicle;

public class Car {

// Class implementation.

Now, put the source file in a directory whose name reflects the name of the package to which the class
belongs −

....\vehicle\[Link]

Now, the qualified class name and pathname would be as follows −

● Class name → [Link]

● Path name → vehicle\[Link] (in windows)

In general, a company uses its reversed Internet domain name for its package names.

Example − A company's Internet domain name is [Link], then all its package names would start with
[Link]. Each component of the package name corresponds to a subdirectory.

Example − The company had a [Link] package that contained a [Link] source file, it
would be contained in a series of subdirectories like this −

....\com\apple\computers\[Link]

At the time of compilation, the compiler creates a different output file for each class, interface and
enumeration defined in it. The base name of the output file is the name of the type, and its extension
is .class.

For example −
// File Name: [Link]

package [Link];

public class Dell {

class Ups {

Now, compile this file as follows using -d option −

$javac -d . [Link]

The files will be compiled as follows −

.\com\apple\computers\[Link]

.\com\apple\computers\[Link]

You can import all the classes or interfaces defined in \com\apple\computers\ as follows −

import [Link].*;

Like the .java source files, the compiled .class files should be in a series of directories that reflect the
package name. However, the path to the .class files does not have to be the same as the path to
the .java source files. You can arrange your source and class directories separately, as −

<path-one>\sources\com\apple\computers\[Link]

<path-two>\classes\com\apple\computers\[Link]

By doing this, it is possible to give access to the classes directory to other programmers without
revealing your sources. You also need to manage source and class files in this manner so that the
compiler and the Java Virtual Machine (JVM) can find all the types your program uses.

The full path to the classes directory, <path-two>\classes, is called the class path, and is set with the
CLASSPATH system variable. Both the compiler and the JVM construct the path to your .class files by
adding the package name to the class path.

Say <path-two>\classes is the class path, and the package name is [Link], then the
compiler and JVM will look for .class files in <path-two>\classes\com\apple\computers.

A class path may include several paths. Multiple paths should be separated by a semicolon (Windows) or
colon (Unix). By default, the compiler and the JVM search the current directory and the JAR file
containing the Java platform classes so that these directories are automatically in the class path.
Set CLASSPATH System Variable

To display the current CLASSPATH variable, use the following commands in Windows and UNIX (Bourne
shell) −

● In Windows → C:\> set CLASSPATH

● In UNIX → % echo $CLASSPATH

To delete the current contents of the CLASSPATH variable, use −

● In Windows → C:\> set CLASSPATH =

● In UNIX → % unset CLASSPATH; export CLASSPATH

To set the CLASSPATH variable −

● In Windows → set CLASSPATH = C:\users\jack\java\classes

● In UNIX → % CLASSPATH = /home/jack/java/classes; export CLASSPAT

Now a package/folder with the name animals will be created in the current directory and these class
files will be placed in it as shown below.

Create Directory

You can execute the class file within the package

$ java [Link]

and get the result as shown below.

Mammal eats

Mammal travels
com/example/[Link]

package [Link];

public class Calculator {

public int add(int a, int b) {

return a + b;

public int subtract(int a, int b) {

return a - b;

public int multiply(int a, int b) {

return a * b;

public int divide(int a, int b) {

if (b != 0) {

return a / b;

} else {

throw new ArithmeticException("Cannot divide by zero!");

Now, create another file outside the "com" folder to access the Calculator class from the user-defined
package.

[Link]

import [Link];

public class PackageExample {


public static void main(String[] args) {

Calculator calculator = new Calculator();

int result = [Link](5, 3);

[Link]("Addition: " + result);

result = [Link](5, 3);

[Link]("Subtraction: " + result);

result = [Link](5, 3);

[Link]("Multiplication: " + result);

result = [Link](10, 2);

[Link]("Division: " + result);

To use an interface, you write a class that implements the interface.

Interfaces in Java

In the Java programming language, an interface is a reference type, similar to a class, that can
contain only constants, method signatures, default methods, static methods, and nested types. Method
bodies exist only for default methods and static methods.

Syntax

interface {
// declare constant fields
// declare methods that abstract
// by default.
}

Class Interface

In class, you can instantiate variables and create In an interface, you must initialize variables as
an object. they are final but you can’t create an object.
Class Interface

A class can contain concrete (with The interface cannot contain concrete (with
implementation) methods implementation) methods.

The access specifiers used with classes are


In Interface only one specifier is used- Public.
private, protected, and public.
consider the example of Vehicles like bicycles, cars, and bikes share common functionalities, which can
be defined in an interface, allowing each class (e.g., Bicycle, Car, Bike) to implement them in its own
way.

import [Link].*;

// Add interface

interface Add{

int add(int a,int b);

// Sub interface

interface Sub{

int sub(int a,int b);

// Calculator class implementing

// Add and Sub

class Cal implements Add , Sub

// Method to add two numbers

public int add(int a,int b){

return a+b;

// Method to sub two numbers

public int sub(int a,int b){

return a-b;

}
}

class GFG{

// Main Method

public static void main (String[] args)

// instance of Cal class

Cal x = new Cal();

[Link]("Addition : " + [Link](2,1));

[Link]("Substraction : " + [Link](2,1));

}
import [Link].*;

interface Vehicle {

// Abstract methods defined

void changeGear(int a);

void speedUp(int a);

void applyBrakes(int a);

// Class implementing vehicle interface

class Bicycle implements Vehicle{

int speed;

int gear;

// Change gear

@Override

public void changeGear(int newGear){

gear = newGear;

// Increase speed

@Override

public void speedUp(int increment){

speed = speed + increment;

// Decrease speed
@Override

public void applyBrakes(int decrement){

speed = speed - decrement;

public void printStates() {

[Link]("speed: " + speed

+ " gear: " + gear);

// Class implementing vehicle interface

class Bike implements Vehicle {

int speed;

int gear;

// Change gear

@Override

public void changeGear(int newGear){

gear = newGear;

// Increase speed

@Override

public void speedUp(int increment){

speed = speed + increment;

}
// Decrease speed

@Override

public void applyBrakes(int decrement){

speed = speed - decrement;

public void printStates() {

[Link]("speed: " + speed

+ " gear: " + gear);

class GFG

public static void main (String[] args)

// Instance of Bicycle(Object)

Bicycle bicycle = new Bicycle();

[Link](2);

[Link](3);

[Link](1);

[Link]("Bicycle present state : ");

[Link]();

// Instance of Bike (Object)


Bike bike = new Bike();

[Link](1);

[Link](4);

[Link](3);

[Link]("Bike present state : ");

[Link]();

You might also like