Skip to main content

Java Packages

Java Packages
It is repeatedly said that one of the main features of the Object Oriented Programming is the ability to reuse the code already written by the programmer. One way of achieving is by extending class and using the interface. It has a limitation. What will you have to do if you have to use a class from another program without physically copying them into the program at the time of development? So another way of achieving the concept of reusability in Java is via the use of Packages. In this chapter, you will learn about how packages are used within a Java program.

What are packages?

Packages in Java are groups of similar types of classes, interface and sub packages. It is a way of grouping a variety of classes or interfaces collectively. The grouping is usually done according to functionality. The Java packages act as containers for Java classes.
There is also a term named sub-packages. Package inside the package is called the sub-package. It should be created to categorize the package further.
Here’s are the benefits of organizing classes:
  1. The classes contained in the packages of another program can be easily reused.
  2. Packages also allow programmers to separate design from coding.
  3. In packages, classes can be declared uniquely compared with classes in other packages.
  4. Java Packages provide a way to ‘hide’ classes thus preventing other programs or packages from accessing classes that are meant for internal use only.
  5. Packages provide access protection.
  6. Java package removes naming collision.
For most applications, programmers need to use different sets of classes, one for the internal representation of program’s data and other for the external presentation purposes. You may build your own classes for handling your program data and use existing class libraries for designing class libraries for designing the user interface.

Creating Packages

While creating a package, programmers must choose a name for the package and include a package statement along with that name at the top of the source program that contains the classes, interfaces, enumerations, and annotation types that you want to include in the package. There can be only one statement of the package in each source code, and it applies to all types in the file.

To compile the Java programs with package programmers have to do used option as shown below

javac -d Destination_folder file_name.java
After that, a folder is created with the given package name in a specific location and the compiled class files will be placed in that folder.
The general form of creating a package is:
package pack_name;
public class firstClass
{
. . . . . . . . //body of the class.
. . . . . . . .
}
Here, the package name is pack_name and the class firstClass is considered a part of this package. This listing would be saved in as a file called firstClass.java and will be stored in the directory named pack_name. When the file will get compiled, Java will create a .class file and store it in the same directory.

Example of Java Program implementing Package

Example:
package weapons;
interface ammo {
public void sniper();
public void rifle();
}

Now we have to implement the above interface in the same package weapons like this

Example:
package weapons;
/* File name : arms.java */
public class arms implements Ammo {
public void sniper() {
System.out.println("Magnum Sniper");
}
public void rifle() {
System.out.println("Sub_Machinegun");
}
public int noOfBullets() {
return 0;
}
public static void main(String args[]) {
arms m
= new arms();
m
.sniper();
m
.rifle();
}
}
Now compile the java files as shown below:
$ javac -d . ammo.java 
$ javac -d . arms.java

Accessing Package

There are three ways to access the package from outside the package:
  • import package.*;
  • import package.classname;
  • fully qualified name.
You can use the import statement when there are many references to a particular package. The same approach can be used to access the user-defined packages as well. The import statement can be used to search a list of packages for a particular class. The general form of import statement for searching a class is:
import package1 [.package2] [.package3].classname;
Here, package1 is the name of the top-level package. package2 is the name of the package that is inside the 1st package i.e. package1 and so on. Programmers can use any number of packages in the package hierarchy. At the end, the explicitly class name is specified.

Comments

For Programs Click Here

Popular posts from this blog

Syllabus

Syllabus  C Programming Tutorials C Tutorial C Introduction History of C Programming Language C Installation C Program Structure C Input and Output (I/O) C Format Specifiers Declaration & Assignments C Tokens C Identifiers C Keywords C Constants C Operators C Data Types C Variables C Preprocessors C Type Casting C Custom Header File Flow Control C Decision Making C if Statements C if-else Statements C Nested if-else Statements C else-if Statements C goto Statement C switch Statements C Loops C while loops C do while loops C for loops Functions C Functions C Function Arguments C Library Functions C Variable Scope Arrays & Strings C Arrays C Strings Pointers C Pointers C Dynamic Memory Allocation Structure & Union C Structures C Unions File I/O C File Handling C fopen C fclose C getc C putc C getw C putw C fprintf C fscanf C fgets C fputs C feof                                     ...

Syllabus

Python Tutorials Python Tutorial Python Overview Python Installation Basics of Python Programming Python Operators Python Keywords Python Numbers Python Strings Python Data Types Python Variables Python Lists Python Tuples Python Date and Time Python Decision Making Python Loops Python File Handling Python Dictionaries Python Functions Python Modules Python Exceptions Handling Python Object Oriented Inheritance in Python Python Regular Expressions Python Networking Programming Python Multithreaded Programming Python CGI Programming Python Database Connection Python Metaprogramming Python Data Processing And Encoding Python GUI Programming

C++ Data Abstraction

C++ Data Abstraction Object oriented programming offers various features to write programs with various concepts that help to minimize problems and increase flexibility in program. One of the features of object oriented programming is Data abstraction. In this chapter you will learn about how the concept data abstraction is carried out within the C++ program. What is Data abstraction? Data abstraction allows a program to ignore the details of how a data type is represented. Abstraction (derived from a Latin word  abs , meaning away from and  trahere , meaning to draw) refers to the act of representing essential features without including the background details or explanations. C++ classes use the technique of abstraction and are defined as a list of abstract attributes such as width, cost, size etc and functions to operate on these attributes. They put in a nutshell all the essential properties of an object that are required to be created. The attributes are therefore called...