Presentation is loading. Please wait.

Presentation is loading. Please wait.

Characteristics of OOP Encapsulation-------chap.5 Inheritance-----------chap.6 Polymorphism-------chap.7.

Similar presentations


Presentation on theme: "Characteristics of OOP Encapsulation-------chap.5 Inheritance-----------chap.6 Polymorphism-------chap.7."— Presentation transcript:

1 Characteristics of OOP Encapsulation-------chap.5 Inheritance-----------chap.6 Polymorphism-------chap.7

2 5: Hiding the Implementation /Access Control package: the library unit –Creating unique package names –Collisions –A custom tool library –Using imports to change behavior –Package caveat Java access specifiers –“Friendly” –public: interface access –The default package –private: you can’t touch that! –protected: “sort of friendly” Interface and implementation Class access Exercises

3 Hiding the Implementation A primary consideration in object-oriented design is “ separating the things that change from the things that stay the same. ” –This is particularly important for libraries. –The user (client programmer) of that library must be able to rely on the part they use, and know that they won ’ t need to rewrite code if a new version of the library comes out. –On the flip side, the library creator must have the freedom to make modifications and improvements with the certainty that the client programmer ’ s code won ’ t be affected by those changes. To solve this problem, Java provides access specifiers – to allow the library creator to say what is available to the client programmer and what is not. –The levels of access control from “ most access ” to “ least access ” are public, protected, “ friendly ” (which has no keyword), and private.

4 Compilation Unit compilation unit: a source-code file for Java, which have a name ending in.java public class: There can be one and there can be only one in each compilation unit. public class name: the same name as the file (including capitalization, but excluding the.java filename extension). If there are additional classes in that compilation unit, they are hidden from the world outside that package because they’re not public, and they comprise “support” classes for the main public class.

5 package: the library unit Absolute class name com.bruceeckel.utility.foibles.AAA a=new com.bruceeckel.utility.foibles.AAA(); Import statement import com.bruceeckel.utility.foibles.* ; AAA a=new AAA(); if you use a package statement, it must appear as the first noncomment in the file. Creating unique package names (why & how)

6 Name visibility/Using other components package com.bruceeckel.utility.foibles ; class AAA {……} ======================= package com.other ; // if omit, means default package import com.bruceeckel.utility.foibles.* ; import java.util.ArrayList; import java.util.*; AAA a=new AAA(); //com.bruceeckel.utility.foibles.AAA a=new com.bruceeckel.utility.foibles.AAA(); ======================= java.lang.* is the exception which is needless to import

7 CLASSPATH environment variable import com.bruceeckel.simple.*; public class LibTest { public static void main(String[] args) { Vector v = new Vector(); List l = new List(); } } ///:~ –When the compiler encounters the import statement, it begins searching at the directories specified by CLASSPATH, looking for subdirectory com\bruceeckel\simple, then seeking the compiled files of the appropriate names (Vector.class for Vector and List.class for List). –CLASSPATH=.;D:\JAVA\LIB;C:\DOC\JavaT –CLASSPATH=.;D:\JAVA\LIB;C:\flavors\grape.jar

8

9 Package: the library unit Package caveat –It ’ s worth remembering that anytime you create a package, you implicitly specify a directory structure when you give the package a name. The package must live in the directory indicated by its name, which must be a directory that is searchable starting from the CLASSPATH. //: com:bruceeckel:simple:Vector.java // Creating a package. package com.bruceeckel.simple; public class Vector { public Vector() { System.out.println("com.bruceeckel.simple.Vector"); } } ///:~

10

11 Class Access Class Access: “ friendly ” or public –Note that a class cannot be private (that would make it accessible to no one but the class), or protected. –Inner class: private protected friendly public

12 Java access specifiers Java access specifiers: public, protected, and private Placed in front of each definition for each member in your class, whether it ’ s a field or a method. Each access specifier controls the access for only that particular definition. This is a distinct contrast to C++ …

13 Java access specifiers “ Friendly ” –The default access has no keyword, but it is commonly referred to as “ friendly. ” –It means that all the other classes in the current package have access to the friendly member, but to all the classes outside of this package the member appears to be private. public: interface access –When you use the public keyword, it means that the member declaration that immediately follows public is available to everyone,

14 Java access specifiers package c05.dessert; public class Cookie { public Cookie() { System.out.println("Cookie constructor"); } void bite() { System.out.println("bite"); } } ///:~ ======================= import c05.dessert.*; public class Dinner { public Dinner() { System.out.println("Dinner constructor"); } public static void main(String[] args) { Cookie x = new Cookie(); //! x.bite(); // Can't access } } ///:~

15 Java access specifiers private: you can ’ t touch that! The private keyword means that no one can access that member except that particular class, inside methods of that class. class Sundae { private Sundae() {} static Sundae makeASundae() { return new Sundae(); } public class IceCream { public static void main(String[] args) { //! Sundae x = new Sundae(); Sundae x = Sundae.makeASundae(); } } ///:~

16 Java access specifiers protected: “ sort of friendly ” The protected keyword deals with a concept called inheritance, package c05.dessert; public class Cookie { public Cookie() { System.out.println("Cookie constructor"); } void bite() { System.out.println("bite"); } } ///:~ ============= import c05.dessert.*; public class ChocolateChip extends Cookie { public ChocolateChip() { System.out.println( "ChocolateChip constructor"); } public static void main(String[] args) { ChocolateChip x = new ChocolateChip(); //! x.bite(); // Can't access bite } } ///:~

17 Java access specifiers package c05.dessert; public class Cookie { public Cookie() { System.out.println("Cookie constructor"); } protected void bite() { System.out.println("bite"); } } ///:~ ============= import c05.dessert.*; public class ChocolateChip extends Cookie { public ChocolateChip() { System.out.println( "ChocolateChip constructor"); } public static void main(String[] args) { ChocolateChip x = new ChocolateChip(); x.bite(); // OK! } } ///:~

18 Example: singleton public class Soup { private Soup() {} // (2) Create a static object and // return a reference upon request. // (The "Singleton" pattern): private static Soup ps1 = new Soup(); public static Soup access() { return ps1; } public void f() {} }

19 Exercises 10. Following the form of the example Lunch.java, create a class called ConnectionManager that manages a fixed array of Connection objects. The client programmer must not be able to explicitly create Connection objects, but can only get them via a static method in ConnectionManager. When the ConnectionManager runs out of objects, it returns a null reference. Test the classes in main( ).


Download ppt "Characteristics of OOP Encapsulation-------chap.5 Inheritance-----------chap.6 Polymorphism-------chap.7."

Similar presentations


Ads by Google