Download Access Control 1 Problem A primary consideration in object

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Access Control
2017/5/24
1
Problem
• A primary consideration in objectoriented design is to “separate the
things that change from the things that
stay the same.”
• Consumers of that library must 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
andimprovements with the certainty
that the client code won’t be affected by
those changes.
2017/5/24
2
Why cannot do it through
convention?
• In the case of a field, how can the
library creator know which fields have
been accessed by client programmers?
• This is also true with methods that are
only part of the implementation of a
class, and not meant to be used
directly by the client programmer.
2017/5/24
3
Access specifiers
• The levels of access control from
“most access” to “least access” are
public, protected, package access
(which has no keyword), and private.
2017/5/24
4
package: the library unit
• A package contains a group of classes, organized
together under a single namespace.
• The reason for all this importing is to provide a
mechanism to manage namespaces.
• When you create a source-code file for Java, it’s
commonly called a compilation unit (sometimes
a translation unit). Each compilation unit must
have a name ending in .java, and inside the
compilation unit there can be a public class that
must have the same name as the file (including
capitalization, but excluding the .java file name
extension).
• There can be only one public class in each
compilation unit
2017/5/24
5
Code organization
• When you compile a .java file, you get
an output file for each class in
the .java file. Each output file has the
name of a class in the .java file, but
with an extension of .class.
• A working program is a bunch of .class
files, which can be packaged and
compressed into a Java archive.
2017/5/24
6
• If you want to say that all these
components (each in its own
separate .java and .class files) belong
together, that’s where the package
keyword comes in.
• If you use a package statement, it
must appear as the first non-comment
in the file.
2017/5/24
7
Creating unique package names
• Place all the .class files for a particular
package into a single directory; that is,
use the hierarchical file structure of the
operating system to your advantage.
• Collecting the package files into a single
subdirectory solves two other problems:
creating unique package names, and
finding those classes that might be
buried in a directory structure
someplace.
2017/5/24
8
Collisions
• suppose a program does this:
• Since java.util.* also contains a Vector
class, this causes a potential collision.
• The collision does occur if you now try
to make a Vector.
• Must explicitly specify the class names.
2017/5/24
9
Using imports to change
behavior
• Java does not have conditional
compilation.
• A very common use of conditional
compilation is for debugging code.
• You can accomplish this by changing the
package that’s imported in order to
change the code used in your program
from the debug version to the roduction
version.
• This technique can be used for any kind
of conditional code.
2017/5/24
10
Package caveat
• 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.
• Note that compiled code is often placed
in a different directory than source code,
but the path to the compiled code must
still be found by the JVM using the
CLASSPATH.
2017/5/24
11
Java access specifiers
• The Java access specifiers public,
protected, and private are placed in
front of each definition for each
member in your class, whether it’s a
field or a method.
• Each access specifier only controls the
access for that particular definition.
• If you don’t provide an access
specifier, it means “package access.”
2017/5/24
12
Package access
• The default access has no keyword, but it is
commonly referred to as package access (and
sometimes “friendly”).
• It means that all the other classes in the
current package have access to that member,
but to all the classes outside of this package,
the member appears to be private.
• Since a compilation unit—a file—can belong
only to a single package, all the classes within
a single compilation unit are automatically
available to each other via package access.
2017/5/24
13
• Package access allows you to group
related classes together in a package
so that they can easily interact with
each other.
2017/5/24
14
grant access to a
member
• Make the member public. Then everybody,
everywhere, can access it.
• Give the member package access by leaving
off any access specifier, and put the other
classes in the same package.
• An inherited class can access a protected
member as well as a public member (but not
private members).
• Provide “accessor/mutator” methods (also
known as “get/set” methods) that read and
change the value. This is the most civilized
approach in terms of OOP
2017/5/24
15
public: interface access
• When you use the public keyword, it
means that the member declaration
that immediately follows public is
available to everyone, in particular to
the client programmer who uses the
library
2017/5/24
16
The default package
• Java treats files that are in the same
directory and have no explicit package
name as implicitly part of the “default
package” for that directory, and thus
they provide package access to all the
other files in that directory.
2017/5/24
17
private: you can’t touch that!
• The private keyword means that no
one can access that member except
the class that contains that member,
inside methods of that class.
• Unless you must expose the
underlying implementation (which is
less likely than you might think), you
should make all fields private.
2017/5/24
18
protected: inheritance access
• The protected keyword deals with a
concept called inheritance, which
takes an existing class— which we
refer to as the base class—and adds
new members to that class without
touching the existing class.
• You can also change the behavior of
existing members of the class.
2017/5/24
19
Interface and implementation
• Access control is often referred to as
implementation hiding.
• Wrapping data and methods within
classes in combination with
implementation hiding is often called
encapsulation.
2017/5/24
20
Class access
• In Java, the access specifiers can also
be used to determine which classes
within a library will be available to the
users of that library. If you want a
class to be available to a client
programmer, you use the public
keyword on the entire class definition.
• To control the access of a class, the
specifier must appear before the
keyword class.
2017/5/24
21
extra set of constraints
• There can be only one public class per
compilation unit (file). The idea is that
each compilation unit has a single public
interface represented by that public
class.
• The name of the public class must
exactly match the name of the file
containing the compilation unit,
including capitalization.
• It is possible, though not typical, to have
a compilation unit with no public class at
all.
2017/5/24
22
Summary
• In any relationship it’s important to have
boundaries that are respected by all
parties involved.
• How classes are built to form libraries:
first, the way a group of classes is
packaged within a library, and second,
the way the class controls access to its
members.
• There are two reasons for controlling
access to members. The first is to keep
users’ hands off portions that they
shouldn’t touch.
2017/5/24
23
• The second and most important reason
for access control is to allow the library
designer to change the internal workings
of the class without worrying about how
it will affect the client programmer.
• The public interface to a class is what the
user does see.
• Access control focuses on a
relationship—and a kind of
communication—between a library
creator and the external clients of that
library.
2017/5/24
24