PackagesPuttingClassesTogether_第1頁(yè)
PackagesPuttingClassesTogether_第2頁(yè)
PackagesPuttingClassesTogether_第3頁(yè)
PackagesPuttingClassesTogether_第4頁(yè)
PackagesPuttingClassesTogether_第5頁(yè)
已閱讀5頁(yè),還剩19頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、Packages: Putting Classes Together1IntroductionThe main feature of OOP is its ability to support the reuse of code:Extending the classes (via inheritance)Extending interfacesThe features in basic form limited to reusing the classes within a program.What if we need to use classes from other programs

2、without physically copying them into the program under development ?In Java, this is achieved by using what is known as “packages”, a concept similar to “class libraries” in other languages.2PackagesPackages are Javas way of grouping a number of related classes and/or interfaces together into a sing

3、le unit. That means, packages act as “containers” for classes.The benefits of organising classes into packages are:The classes contained in the packages of other programs/applications can be reused.In packages classes can be unique compared with classes in other packages. That two classes in two dif

4、ferent packages can have the same name. If there is a naming clash, then classes can be accessed with their fully qualified name.Classes in packages can be hidden if we dont want other packages to access them.Packages also provide a way for separating “design” from coding.3Java Foundation PackagesJa

5、va provides a large number of classes groped into different packages based on their functionality.The six foundation Java packages are:java.langContains classes for primitive types, strings, math functions, threads, and exceptionjava.utilContains classes such as vectors, hash tables, date etc.java.i

6、oStream classes for I/Ojava.awtClasses for implementing GUI windows, buttons, menus Classes for networkingjava.appletClasses for creating and implementing applets4Using System PackagesThe packages are organised in a hierarchical structure. For example, a package named “java” contains the package “aw

7、t”, which in turn contains various classes required for implementing GUI (graphical user interface).GraphicsFontjavaImageawtlang“java” Package containing“l(fā)ang”, “awt”,. packages;Can also contain classes.awt Package containingclassesClasses containing methods5Accessing Classes from PackagesThere are

8、two ways of accessing the classes stored in packages:Using fully qualified class namejava.lang.Math.sqrt(x);Import package and use class name directly.import java.lang.MathMath.sqrt(x);Selected or all classes in packages can be imported:Implicit in all programs: import java.lang.*;package statement(

9、s) must appear first import package.class;import package.*;6Creating PackagesJava supports a keyword called “package” for creating user-defined packages. The package statement must be the first statement in a Java source file (except comments and white spaces) followed by one or more classes. Packag

10、e name is “myPackage” and classes are considred as part of this package; The code is saved in a “ClassA.java” and located in a directory called “myPackage”.package myPackage;public class ClassA / class bodyclass ClassB / class body7Creating Sub PackagesClasses in one ore more source files can be par

11、t of the same packages.As packages in Java are organised hierarchically, sub-packages can be created as follows:package myPackage.Mathpackage myPackage.secondPakage.thirdPackageStore “thirdPackage” in a subdirectory named “myPackagesecondPackage”. Store “secondPackage” and “Math” class in a subdirec

12、tory “myPackage”.8Accessing a PackageAs indicated earlier, classes in packages can be accessed using a fully qualified name or using a short-cut as long as we import a corresponding package.The general form of importing package is:import package1.package2.classnameExample:import myPackage.ClassA;imp

13、ort myPackage.secondPackageAll classes/packages from higher-level package can be imported as follows:import myPackage.*;9Using a PackageLet us store the code listing below in a “ClassA.java” within subdirectory named “myPackage” within the current directory (say “abc”).package myPackage;public class

14、 ClassA / class body public void display() System.out.println(Hello, I am ClassA); class ClassB / class body10Using a PackageWithin the current directory (“abc”) store the following code in a “ClassX.java”import myPackage.ClassA;public class ClassX public static void main(String args) ClassA objA =

15、new ClassA(); objA.display(); 11Compiling and RunningWhen ClassX.java is compiled, the compiler compiles it and places .class current directly. If .class of ClassA in subdirectory “myPackage” is not found, it comples ClassA also. Note: It does not include code of ClassA into ClassXWhen the program C

16、lassX is run, java loader looks for ClassA.class a package called “myPackage” and loads it.12Using a PackageLet us store the code listing below in a “ClassA.java” within subdirectory named “secondPackage” within the current directory (say “abc”).package secondPackage;public class ClassC / class body

17、 public void display() System.out.println(Hello, I am ClassC); 13Using a PackageWithin the current directory (“abc”) store the following code in a “ClassX.java”import myPackage.ClassA;import secondPackage.ClassC;public class ClassY public static void main(String args) ClassA objA = new ClassA(); Cla

18、ssC objC = new ClassC(); objA.display(); objC.display(); 14Outputrajmundroo package % java ClassYHello, I am ClassAHello, I am ClassCrajmundroo package % 15Protection and PackagesAll classes (or interfaces) accessible to all others in the same package.Class declared public in one package is accessib

19、le within another. Non-public class is notMembers of a class are accessible from a difference class, as long as they are not privateprotected members of a class in a package are accessible to subclasses in a different class16Visibility - RevisitedPublic keyword applied to a class, makes it available

20、/visible everywhere. Applied to a method or variable, completely visible.Private fields or methods for a class only visible within that class. Private members are not visible within subclasses, and are not inherited.Protected members of a class are visible within the class, subclasses and also withi

21、n all classes that are in the same package as that class.17Visibility Modifiers18Adding a Class to a PackageConsider an existing package that contains a class called “Teacher”:This class is stored in “Teacher.java” file within a directory called “pack1”.How do we a new public class called “Student”

22、to this package.package pack1;public class Teacher / class body19Adding a Class to a PackageDefine the public class “Student” and place the package statement before the class definition as follows:Store this in “Student.java” the directory “pack1”.When the “Student.java” compiled, the class be creat

23、ed and stored in the directory “pack1”. Now, the package “pack1” will contain both the classes “Teacher” and “Student”.package pack1;public class Student / class bodyclass Teacherpackage pack1;class Student20Packages and Name ClashingWhen packages are developed by different organizations, it is poss

24、ible that multiple packages will have classes with the same name, leading to name classing. We can import and use these packages like:import pack1.*;import pack2.*;Student student1; / Generates compilation errorclass Teacherpackage pack1;class Studentclass Studentpackage pack2;class Courses21Handling Name ClashingIn Java, name classing is

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論