java packages oop

26
Welcome to our presentation…………. .

Upload: sumon-jr

Post on 17-Aug-2015

76 views

Category:

Engineering


1 download

TRANSCRIPT

Page 1: Java packages oop

Welcome to our presentation…………..

Page 2: Java packages oop

Group membersName IDMd.Ilias Bappi 131-15-2266Md.Kawsar Hamid 131-15-2223Shahabuddin Ringkon 131-15-2343Bristy Roy 131-15-2421Janntul ferdous promy 131-15-2347

Page 3: Java packages oop

Topic :-

Presentation on Java Packages

Page 4: Java packages oop

Packages

1. Package is a container for classes.

2.A package is a grouping of related types (classes and interfaces) providing access protection and name

space management.

3.In simple words,package is the way we organize files into different directories according to their

functionality,usability as well as category they should belong to.

Page 5: Java packages oop

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.

Page 6: Java packages oop

How To Create a PackagePackages are mirrored through

directory structure.To create a package, First we have to

create a directory /directory structure that matches the package hierarchy.

Package structure should match the directory structure also.

To make a class belongs to a particular package include the package statement as the first statement of source file.

Page 7: Java packages oop

Exercise Creating Packagesmypackage

mypackageBmypackageA

ABC DEG IJK XYZ

A,B,C D,E,F A,B,C,I,J,K

X,Y,Z

Package ABC and IJK have classes with same name.

A class in ABC has name mypackage.mypackageA.ABC.A

A class in IJK has name mypackage.mypackageB.IJK.A

Page 8: Java packages oop

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();

Page 9: Java packages oop

Using PackagesIf you want to use a built-in Java class, first look it

up in the Java Application Interface. (API)You can find the Java API documentation on the

course website page.http://www.tihe.org/courses/it151

This is the API entry for the Graphics class. The package for this class is java.awt.

Page 10: Java packages oop

Using Packages (cont.)Once you know the package for a class,

import it into your program.

To import all classes in a package, use the wild card (*).

import java.awt.Graphics;

import java.awt.*;

Page 11: Java packages oop

Access ProtectionJava addresses four categories of visibility for

class members:

• Subclasses in the same package• Non-subclasses in the same package• Subclasses in different packages• Classes that are neither in the same package

nor subclasses

Page 12: Java packages oop

Access to members of the classes

Java packages can be stored in compressed files called JAR files,

allowing classes to download faster as a group rather than one at a time.

Page 13: Java packages oop

Defining a PackageIf you want to make your own package to

be used in other programs, define it.

Defining packages can lead to problems compiling your program.

If you have multiple files in your project, make sure they are all defined in the same package.

package myprojects.helloworld;

Page 14: Java packages oop

Java files for graphics

//in the Shape.java file Public abstract class Shape { . . . } //in the Rectangle .java file Public class Rectangle extends Shape { . . . }

Page 15: Java packages oop

Package graphics : 1st StepChoose a name for the package (graphics,for example) and put a package statement with that name at the top of every source file that contains

the classes that you want to include in the package.

2. In the Shape.java file : Package graphics; Public abstract class shape { . . . } 3. in the Rectangle.java file :

Package graphics; public class Rectangle extends Shape { . . . }

Page 16: Java packages oop

Package graphics : 2nd stepPut the source files in a directionary whose name

(graphics,for example)reflects the name of the package to which the type belongs :

  . . ./ graphics/Shape.java . . ./ graphics/Circle.java . . ./ graphics/Rectangle.java . . ./ graphics/Cylinder.java Etc.

Page 17: Java packages oop

Package Name & Package FolderA company uses its reserved Internet domain name for its

package names . The ABC company , whose Internet domain name is ABC.com , would precede all its package names withcom.ABC

 Package com.ABC.graphics;

2.Each component of the package name corresponds to a subdirectory.So,if the ABC company had a com.ABC.graphics package that contained a Shape.java source file,it would be contained in a series of subdirectories like this ;

…/com/ABC/graphics/Shape.java

…/com/ABC/graphics/Circle.java

Page 18: Java packages oop

Example package Suppose you write a group of classes that represent

graphic objects,such as circles,rectangles,lines & points.

You also write an interface,Draggable that classes implement if they can be dragged with the mouse

//in the Draggable.java file public interface Draggable {} //in the Graphic.java file public abstract class Graphic{}  //in the Circle.java file public class Circle extends Graphic implements

Draggable {} 

Page 19: Java packages oop

Example package

//in the Rectangle.java file Public class Rectangle extends Graphic implements

Draggable {}  //in the Point.java file Public class Point extends Graphic implements

Draggable {}  //in the Line.java file Public class Line extends Graphic implements Draggable

{}  

Page 20: Java packages oop

Example : Placing StudentRecord class in Schoolclasses package 

Package SchoolClasses;  Public class StudentRecord { Private String name ; Private String address; Private int age; : 

Page 21: Java packages oop

Importing and Using classes from other packages

Page 22: Java packages oop

Using Classes from other packagesTo use a public package member (classes &

interfaces)from outside its package,you must do one of the following :-

- Import the package member using import statement.

- Import the member’s entire package using import statement.

- Refer to the member by its fully qualified name (without using import statement)

Page 23: Java packages oop

Importing packagesTo be able to use classes outside of the package

you are currently working in , you need to import the package of those classes.

By default, all your Java programs import the java.lang.* package,that is why you can use classes like String & Integers inside the program even though you haven’t imported any packages .

The syntax importing packages is as follows : Import <nameofPackage>;

Page 24: Java packages oop

Example : Importing a Class or a Package

/ / Importing a class import java.util.Date;  / / Importing all classes in the   / / java. Util package   Import java . utill.*;

Page 25: Java packages oop

Benefits of packaging

You and other programmers can easily determine that these classes and interface are related.

You and other programmers know where to find classes and interfaces that can provide graphics-related functions.

The name of your classes and interfaces won’t conflict with the names in other packages because the package creates a new space.

You can allow classes within the package to have unrestricted access to one another yet still restrict access for types outside the package.

Page 26: Java packages oop

ThAnKs AlL