版權(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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 合伙門(mén)窗店合同范例
- 充電樁鋪設(shè)合同模板
- 售后整體返租合同范例
- 客服雇傭合同范例
- 度審計(jì)業(yè)務(wù)合同范例
- 醫(yī)用口罩銷(xiāo)售合同范例
- 鹵味店租房合同模板
- 外賣(mài)干股合同范例
- 小區(qū)循環(huán)疏通合同范例
- 【教案】流體壓強(qiáng)與流速的關(guān)系(教學(xué)設(shè)計(jì))人教版物理八年級(jí)下學(xué)期
- 2024年新教科版八年級(jí)上冊(cè)物理課件 第6章 質(zhì)量與密度 4.跨學(xué)科實(shí)踐:密度應(yīng)用交流會(huì)
- 2025屆廣西桂林十八中高三(最后沖刺)物理試卷含解析
- 廣東省東莞市2023-2024年七年級(jí)上學(xué)期語(yǔ)文期中考試試卷(含答案)
- 《寂靜的春天》參考課件8
- 1例腦梗死恢復(fù)期患者循證護(hù)理個(gè)案分享
- 2024年教師培訓(xùn)講座“三新”背景下的高中英語(yǔ)單元語(yǔ)篇教學(xué)探究與實(shí)踐課件
- Unit 2 Section B (1a-1d)教學(xué)設(shè)計(jì) 2023-2024學(xué)年人教版英語(yǔ)七年級(jí)上冊(cè)
- 黑龍江省綏化市2024屆中考數(shù)學(xué)試卷(含答案)
- 財(cái)務(wù)報(bào)表模板(全套)
- 汽車(chē)臨時(shí)過(guò)戶(hù)協(xié)議書(shū)范本
評(píng)論
0/150
提交評(píng)論