Access Control - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

Access Control

Description:

Access Control * * Problem A primary consideration in object-oriented design is to separate the things that change from the things that stay the same. – PowerPoint PPT presentation

Number of Views:64
Avg rating:3.0/5.0
Slides: 25
Provided by: Virt71
Category:

less

Transcript and Presenter's Notes

Title: Access Control


1
  • Access Control

2
Problem
  • A primary consideration in object-oriented 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 wont 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 wont be affected by those changes.

3
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.

4
Access specifiers
  • The levels of access control from most access
    to least access are public, protected, package
    access (which has no keyword), and private.

5
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, its
    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

6
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.

7
  • If you want to say that all these components
    (each in its own separate .java and .class files)
    belong together, thats where the package keyword
    comes in.
  • If you use a package statement, it must appear as
    the first non-comment in the file.

8
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.

9
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.

10
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
    thats 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.

11
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.

12
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 its a
    field or a method.
  • Each access specifier only controls the access
    for that particular definition.
  • If you dont provide an access specifier, it
    means package access.

13
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 unita filecan belong only
    to a single package, all the classes within a
    single compilation unit are automatically
    available to each other via package access.

14
  • Package access allows you to group related
    classes together in a package so that they can
    easily interact with each other.

15
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

16
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

17
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.

18
private you cant 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.

19
protected inheritance access
  • The protected keyword deals with a concept called
    inheritance, which takes an existing class which
    we refer to as the base classand adds new
    members to that class without touching the
    existing class.
  • You can also change the behavior of existing
    members of the class.

20
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.

21
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.

22
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.

23
Summary
  • In any relationship its 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 shouldnt touch.

24
  • 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 relationshipand a
    kind of communicationbetween a library creator
    and the external clients of that library.
Write a Comment
User Comments (0)
About PowerShow.com