-2

As a student I am beginning to delve deeper into the programming language of Java and understanding the benefits of object-oriented programming. However, I notice I never take advantage of using packages.

My question is what are the advantages of using packages in Java? What are the disadvantages? When should I use them and when should I not?

CodeDucator20
  • 27
  • 1
  • 4
  • 2
    You can create a class called `AwesomClass` and I can create a class called `AweomeClass`, if they reside in different packages, they can reside together within the same VM, otherwise, you have a name clash. Packages provide naming context. (There's also some security management around the base package name space) – MadProgrammer Feb 17 '15 at 04:47
  • 1
    lots of material is available on Google. – Naman Gala Feb 17 '15 at 04:57
  • possible duplicate of http://stackoverflow.com/questions/1088509/what-is-the-purpose-of-defining-a-package-in-java-file – Naman Gala Feb 17 '15 at 04:58
  • @MadProgrammer thank you for the comment, that make sense now as to classification of classes that can have the same name, but reside in different packages that may contain different meanings. As for the security management is there a way for one package to access contents in the other package? Say my `AwesomClass` tries to access your `AwesomClass`? – CodeDucator20 Feb 17 '15 at 14:19
  • The security mechanism relates to the core api, that is,you can't use java.lang package name space for example, this protects the core api – MadProgrammer Feb 17 '15 at 20:34

4 Answers4

2

Various advantages are(source) :

(1) Packages can contain hidden classes that are used by the package but are not visible or accessible outside the package.

(2) Classes in packages can have fields and methods that are visible by all classes inside the package, but not outside.

(3) Different packages can have classes with the same name. For example, java.awt.Frame and photo.Frame.

You can google for more.

codingenious
  • 8,385
  • 12
  • 60
  • 90
2

What Is a Package?

A package is a namespace that organizes a set of related classes and interfaces. Conceptually you can think of packages as being similar to different folders on your computer. You might keep HTML pages in one folder, images in another, and scripts or applications in yet another. Because software written in the Java programming language can be composed of hundreds or thousands of individual classes, it makes sense to keep things organized by placing related classes and interfaces into packages.

Reference

In general we use packages to resolve naming conflicts. In java API itself you will find classes sharing same name. For example Date class. Java API has java.util.Date and java.sql.Date

EDIT

In an organization development is not one person job. Sometimes project/product is divided into small parts which are done by several other isolated/outsourced team. In such cases the developer may not know the existence of class/interface.

Apart from this, let say you have class Account which is already defined in a package and you need to need some more functionality to it. Now since it is already in use changes to it are not allowed. Here you are left with few options among which creating a new class is one simple option.

0

Packages resolves ambiguity for classes which have the same name.

Use them to give proper structure in project so that every person in the project can understand that all controller I have to write in com.deep.controller package and all DAO related classes I have to write in com.deep.dao package.

user207421
  • 305,947
  • 44
  • 307
  • 483
0

There can be various reasons why people use packages in java; but personally, I would like to mention three of the most important ones.

  1. Code reuse is the first and most important reason why I use packages in java. The Inheritance concept allows you to acquire all the properties and behaviors of a parent object. This means that you can create new classes based on existing classes. When you inherit from an existing class, you can reuse methods and fields of the parent class. Simply put, you can reuse the functionality of a written code by inheriting the properties of the written code into other parts of the code. When I was working on some projects in Java, some of the methods (also known as functions in C++) were almost the same. It could have been a waste of time rewriting these methods; the best option was to reuse the functionality of the code and change only the parameters or some values. So, as a package is a collection of related classes, it becomes easier to reuse the functionality of some code whenever you want to perform similar tasks. You simply import the package and use the class.

  2. Secondly, using packages makes it easier to locate related classes. This has something to do with how your code is organized. For example, in large java projects, where we have hundreds of classes, it is always necessary to group similar types of classes into a package so that you can organize and manage your project; this makes it easier to locate related classes, which improves the efficiency.

  3. The third and final reason why I use packages in java is to prevent naming conflicts. We can define two classes with the same name in different packages. So, to avoid name collision, we can use packages.

desertnaut
  • 57,590
  • 26
  • 140
  • 166
  • If you put a put a blank line above the first bullet number (`1.`), it will automatically display in a list. – Thomas F Mar 13 '20 at 16:19