Packages in Java and how to use them || Types of packages (User defined and Built-in) in Java

Опубликовано: 04 Октябрь 2024
на канале: Learning and Teaching Coding
284
24

A package as the name suggests is a pack(group) of classes, interfaces and other packages. In java we use packages to organize our classes and interfaces.

=== Advantages of using a package in Java ===

[1] Reusability :
While developing a project in java, we often feel that there are few things that we are writing again and again in our code.

Using packages, you can create such things in form of classes inside a package and whenever you need to perform that same task, just import that package and use the class.

[2] Better Organization :
In large java projects where we have several hundreds of classes, it is always required to group the similar types of classes in a meaningful package name so that you can organize your project better and when you need something you can quickly locate it and use it, which improves the efficiency.

[3] Name Conflicts :
We can define two classes with the same name in different packages so to avoid name collision, we can use packages.

=== Types of packages in Java ===
We have two types of packages in java.
1) User defined package: The package we create is called user-defined package.
2) Built-in package: The already defined package like java.io.*, java.lang.* etc are known as built-in packages.

In java we have several built-in packages, for example when we need user input, we import a package like this

For example :
import java.util.Scanner.*;
import java.lang.*;
import java.util.*;
import java.io.*;

Now explanation about package :-
→ java is a top level package
→ util is a sub package
→ and Scanner is a class which is present in the sub package util.

=== Sub packages in Java ===
A package inside another package is known as sub package.

For example :
package Calculator.MathsPack;

=== Points to remember: ===
1) Sometimes class name conflict may occur.

For example :
Lets say we have two packages Calculator and MathsPack and both the packages have a class with the same name, let it be Addition.java.

Now suppose a class import both these packages like this:
import Calculator .*;
import Calculator .MathsPack .*;

This will throw compilation error. To avoid such errors you need to use the fully qualified name method that I have shown here :

Calculator.Addition a = new Calculator.Addition();

In This way you can avoid the import package statements and avoid that name conflict error.

2) I have already discussed this above, let me mention it again here. If we create a class inside a package while importing another package then the package declaration should be the first statement, followed by package import.

For example :
package Calculator;
import Demo.*;

3) A class can have only one package declaration but it can have more than one package import statements.

For example :
package Calculator;
import Calculator.MathsPack.*;
import Demo.*;

#LearningandTeachingCoding