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

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

Embed Size (px)

Citation preview

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

Characteristics of OOP

• Encapsulation-------chap.5

• Inheritance-----------chap.6

• Polymorphism-------chap.7

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

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

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

Hiding the Implementation• A primary consideration in object-oriented design is “se

parating 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 rel

y 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 clien

t programmer and what is not. – The levels of access control from “most access” to “least acce

ss” are public, protected, “friendly” (which has no keyword), and private.

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

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.

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

package: the library unit

• Absolute class namecom.bruceeckel.utility.foibles.AAA a=new com.bruceeck

el.utility.foibles.AAA();

• Import statementimport 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)

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

Name visibility/Using other componentspackage com.bruceeckel.utility.foibles ;class AAA {……}

=======================package com.other ; // if omit, means default packageimport com.bruceeckel.utility.foibles.* ;import java.util.ArrayList; import java.util.*; AAA a=new AAA();//com.bruceeckel.utility.foibles.AAA a=new com.bruceec

kel.utility.foibles.AAA();=======================

java.lang.* is the exception which is needless to import

Page 7: Characteristics of OOP Encapsulation-------chap.5 Inheritance-----------chap.6 Polymorphism-------chap.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

Page 8: Characteristics of OOP Encapsulation-------chap.5 Inheritance-----------chap.6 Polymorphism-------chap.7
Page 9: Characteristics of OOP Encapsulation-------chap.5 Inheritance-----------chap.6 Polymorphism-------chap.7

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"); }

} ///:~

Page 10: Characteristics of OOP Encapsulation-------chap.5 Inheritance-----------chap.6 Polymorphism-------chap.7
Page 11: Characteristics of OOP Encapsulation-------chap.5 Inheritance-----------chap.6 Polymorphism-------chap.7

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

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

Java access specifiers• Java access specifiers: public, protected, and private • Placed in front of each definition for each member in yo

ur 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++…

public protected default private

public ALL1.inherit2.package

package inside class

default package package package inside class

access specifier

class

accessible

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

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 th

e member declaration that immediately follows public is available to everyone,

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

Java access specifierspackage 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

} } ///:~

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

Java access specifiersprivate: you can’t touch that!The private keyword means that no one can access that member ex

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

} } ///:~

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

Java access specifiersprotected: “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

} } ///:~

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

Java access specifierspackage 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!

} } ///:~

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

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() {}

}

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

Exercises10. Following the form of the example Lunch.java, create a class c

alled 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( ).