0% found this document useful (0 votes)
2 views4 pages

Java Packages Exam Notes

Uploaded by

Avik Chowdhury
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)
2 views4 pages

Java Packages Exam Notes

Uploaded by

Avik Chowdhury
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 Packages

Exam Notes | Total: 20 marks

Q1. What is a Package? (2 marks)


A package in Java is a mechanism used to group related classes, interfaces, and sub-packages
together under one name. It works like a folder/directory that organises files, helping to avoid naming
conflicts and making classes easier to locate, maintain, and reuse.
Real-life example: Just like a mobile phone stores "Photos" in one folder and "Videos" in another, Java
stores related classes in packages — e.g. all banking-related classes can be kept in a package called
bank.

package bank;

Q2. How do we design / create our own package? (5 marks)


Suppose you want to create two Java files and put them in a package. The steps are:

Main Java File & Class File

↓ creates

Package - java file


Class file

Step 1: Declare the package at the beginning of the file


using the form:

package packagename;

Step 2: Define the class to be put in the package, and declare it public.

package myapp;

public class Student {


void display() {
[Link]("Inside myapp package");
}
}

Step 3: Create a subdirectory under the directory where the main source file is stored. The
subdirectory name must match the package name.
Step 4: Store the listing as the [Link] file inside the subdirectory created.

myapp/
[Link]

Step 5: Compile the file — this creates the corresponding .class file inside the subdirectory.

javac -d . [Link]
myapp/
[Link]
[Link]

Step 6: Create a main class, import the package, and use it.
Write a separate program (outside the package folder) that imports the package and uses its class.

import [Link];

class Main {
public static void main(String args[]) {
Student s = new Student();
[Link]();
}
}

Compile and run:

javac [Link]
java Main

Output:

Inside myapp package

Real-life example: It is like a company creating an HR department (package) with its records (class),
and a manager (main class) importing / accessing that department's records to actually use them — the
final result (output) is the work getting done using those records.

Q3. How do we add a class or interface to a package? Give examples (5


marks)
A class or interface is added to a package by declaring the package statement as the first line of the file,
then compiling it so the .class file is placed in the matching folder.
A) Adding an interface to a package

package shapes;

public interface Drawable {


void draw();
}

Compile with:

javac -d . [Link]

B) Adding a class to a package


(implementing the interface above, so both are tied together)

package shapes;

public class Circle implements Drawable {


public void draw() {
[Link]("Drawing a Circle");
}
}

Compile with:
javac -d . [Link]

Both .class files now sit inside the same shapes/ folder:

shapes/
[Link]
[Link]

C) Using the class and interface in a program


A separate program (outside the package folder) imports both and uses them.

import [Link];
import [Link];

class Main {
public static void main(String args[]) {
Drawable d = new Circle();
[Link]();
}
}

Compile and run:

javac [Link]
java Main

Output:

Drawing a Circle

Important rule: The class or interface must be declared public if it needs to be accessed from outside
its package (using import). Without public, it stays accessible only within the same package.
Real-life example: Adding a new employee's file to the "HR" folder — you label the file as belonging to
HR and place it there; it instantly becomes part of HR's records. But if that record needs to be visible to
other departments too, it has to be officially marked "shared" (public) — otherwise it stays private to HR.

Q4. Advantages of using a Package in Java (4 marks)


1 Avoids naming conflicts — classes with the same name can exist in different packages without
clashing (e.g. [Link] and [Link]).
2 Access protection — packages allow control over which classes/members are accessible from
outside, using access modifiers (public, protected, default, private).
3 Reusability — once a package is created, its classes can be reused in multiple programs/projects via
import.
4 Easy to locate and maintain — related classes are grouped together, making large projects easier to
organise, search, and maintain.

Q5. What is the first keyword used in Java application development? (2


marks)
The keyword package is the first keyword used (when present) in a Java source file. It must appear as
the very first statement, even before import statements, because it tells the compiler which package the
class belongs to.
package mypackage; // must be the first line
import [Link].*; // comes after package statement
class Demo { }

Q6. Naming convention for declaring a user-defined package (2 marks)


• Package names should be written in lowercase letters to avoid conflict with class/interface names.
• Words should not contain spaces; use dots (.) to separate sub-package levels (hierarchical naming).
• To ensure global uniqueness, organisations use the reversed domain name as a prefix.

Example:

package [Link];

Here, [Link] follows the reverse-domain convention used by companies (e.g.


[Link]).

You might also like