Access Control 2015-9-31. Problem A primary consideration in object- oriented design is to “separate the things that change from the things that stay.

Slides:



Advertisements
Similar presentations
1 Packages: Putting Classes Together. 2 Introduction The main feature of OOP is its ability to support the reuse of code: Extending the classes (via inheritance)
Advertisements

Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Characteristics of OOP Encapsulation chap.5 Inheritance chap.6 Polymorphism chap.7.
Classes and Object- Oriented... tMyn1 Classes and Object-Oriented Programming The essence of object-oriented programming is that you write programs in.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
Road Map Introduction to object oriented programming. Classes
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Modularity –Keeps the complexity of a.
EEC-681/781 Distributed Computing Systems Java Tutorial Wenbing Zhao Cleveland State University
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
7M701 1 Class Diagram advanced concepts. 7M701 2 Characteristics of Object Oriented Design (OOD) objectData and operations (functions) are combined 
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
CS 2511 Fall  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance  Polymorphism.
Unit 051 Packages What is a Package? Why use Packages? Creating a Package Naming a Package Using Package Members Managing Source and Class Files Visibility.
Applying OO Concepts Using Java. In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The.
IT PUTS THE ++ IN C++ Object Oriented Programming.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA ‏ Packages.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
An Object-Oriented Approach to Programming Logic and Design
1 Lecture 2: Object Oriented Programming in Java.
You gotta be cool. Introduction to Classes, Objects and Strings Introduction Defining a Class with a Member Function Defining a Member Function with a.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Defining New Types Lecture 21 Hartmut Kaiser
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
CS 61B Data Structures and Programming Methodology July 3, 2008 David Sun.
Class Libraries Chapter 1 1 Source Intro to Java Programming Y. Daniel Liang.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
Interfaces About Interfaces Interfaces and abstract classes provide more structured way to separate interface from implementation
JAVA INTRODUCTION. What is Java? 1. Java is a Pure Object – Oriented language 2. Java is developing by existing languages like C and C++. How Java Differs.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
CITA 342 Section 1 Object Oriented Programming (OOP)
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
Packages. Access Specifications Public Available anywhere (public keyword) Only one public class per file allowed Protected Available in subclasses, and.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Classes, Interfaces and Packages
Programming in java Packages Access Protection Importing packages Java program structure Interfaces Why interface Defining interface Accessing impln thru.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Packages. The main feature of OOP is its ability to support the reuse of code: –Extending the classes –Extending interfaces The features in basic form.
CSI 3125, Preliminaries, page 1 Packages. CSI 3125, Preliminaries, page 2 Packages Packages are containers for classes Collection of classes Packages.
Java Package Advantage of Java Package
UNIT-3 Interfaces & Packages. What is an Interface? An interface is similar to an abstract class with the following exceptions: All methods defined in.
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
ENCAPSULATION. WHY ENCAPSULATE? So far, the objects we have designed have all of their methods and variables visible to any part of the program that has.
Chapter 11: Abstract Data Types Lecture # 17. Chapter 11 Topics The Concept of Abstraction Advantages of Abstract Data Types Design Issues for Abstract.
External Scope CECS 277 Mimi Opkins.
Data Abstraction: The Walls
Classes (Part 1) Lecture 3
Chapter 3: Using Methods, Classes, and Objects
Classes and Objects Encapsulation
Packages and Interfaces
Interfaces.
More Object-Oriented Programming
COP 3330 Object-oriented Programming in C++
Object-Oriented Programming
Presentation transcript:

Access Control

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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