Java packages

7,013
-1

Published on

Gives You the brief idea about packages in JAVA
By N.V.Raja Sekhar Reddy
www.technolamp.co.in
Want more interesting...
Watch and Like us @ https://www.facebook.com/Technolamp.co.in
subscribe videos @ http://www.youtube.com/user/nvrajasekhar

Published in: Technology
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
7,013
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
473
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

Java packages

  1. 1. JAVAPACKAGES
  2. 2. Packages Package is a container for classes A package is a grouping of related types(classes and interfaces) providing accessprotection and name space management. In simple words, packages is the way weorganize files into different directories accordingto their functionality, usability as well as categorythey should belong to.
  3. 3. PackagesThe JDK package from SUN: java.net
  4. 4. Why do we need Packages?• One can easily determine that these types are related.• One knows where to find types that can provide task-related functions.• The names of your types wont conflict with the typenames in other packages because the package creates anew namespace.• One can allow types within the package to haveunrestricted access to one another yet still restrict accessfor types outside the package.
  5. 5. Java files for graphics//in the Shape.java filepublic abstract class Shape { . . .}//in the Circle.java filepublic class Circle extends Shape { . . .}//in the Rectangle.java filepublic class Rectangle extends Shape { . . .}
  6. 6. Package graphics: 1st step• Choose a name for the package (graphics, for example) graphicsand put a package statement with that name at the top ofevery source file that contains the classes that you want toinclude in the package.• In the Shape.java file:package graphics;public abstract class Shape { . . .}…• In the Rectangle.java file:package graphics;public class Rectangle extends Shape{ . . .}
  7. 7. Package graphics: 2nd stepPut the source files in a directory whose name (graphics,for example) reflects the name of the package to whichthe type belongs:...graphicsShape.java...graphicsCircle.java...graphicsRectangle.java...graphicsCylinder.javaetc.
  8. 8. Package Name & Package Folder1) A company uses its reversed Internet domain name for its packagenames. The ABC company, whose Internet domain name isABC.com, would precede all its package names with com.ABC package com.ABC.graphics;2) Each component of the package name corresponds to asubdirectory. So, if the ABC company had a com.ABC.graphicspackage that contained a Shape.java source file, it would be containedin a series of subdirectories like this: ....comABCgraphicsShape.java ....comABCgraphicsCircle.java . . .
  9. 9. How to use packages1. Referring to a package member by its qualified name:graphics.Circle myCircle = new graphics.Circle();2. Importing a package member:import graphics.Circle;...Circle myCircle = new Circle();graphics.Rectangle myR = new graphics.Rectangle();3. Importing an entire package:import graphics.*;...Circle myCircle = new Circle();Rectangle myRectangle = new Rectangle();
  10. 10. Access to members of the classes private (default) protected public The same class Yes Yes Yes Yes Subclass in the package No Yes Yes Yes Non-subclass in the package No Yes Yes Yes Subclass in another package No No Yes Yes Non-subclass in another package No No No YesJava packages can be stored in compressed files called JAR files,allowing classes to download faster as a group rather than one at a time.
  11. 11. Access modifier privatepackage p1 package p2 class C1 class C2 extends C1 private int x x cannot be read or modified in C2 class C3 class C4 C1 c1; C1 c1; c1.x cannot be read c1.x cannot be read or modified nor modified
  12. 12. Default access modifierpackage p1 package p2 class C1 class C2 extends C1 int x x cannot be read or modified in C2 class C3 class C4 C1 c1; C1 c1; c1.x can be read or c1.x cannot be read modified nor modified
  13. 13. Access modifier protected package p1 package p2 class C1 class C2 extends C1 protected int x x can be read or modified in C2 class C3 class C4 C1 c1; C1 c1; c1.x can be read or c1.x cannot be read modified nor modified
  14. 14. Access modifier publicpackage p1 package p2 class C1 class C2 extends C1 public int x x can be read or modified in C2 class C3 class C4 C1 c1; C1 c1; c1.x can be read or c1.x can be read nor modified modified

×