java2 - COP 3503 Computer Science II Java Notes#2 More...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Introduction In the last set of Java notes you were introduced to the basics of classes and methods in the Java language. In this set of notes we will take a closer look at some of the details for controlling access to class members. The class provides two major benefits in support of encapsulation: (1) it links the data with the code that manipulates that data and (2) it provides the means through which access to the members of the class can be controlled (this is the most interesting aspect from our point of view and it is what we will focus on in the first part of this week’s lab). There are basically two different types of class members in Java: (1) private members and (2) public members. [Note: actually Java is a bit more complicated than this, but for now let’s assume that there are only two types of classes.] A public member can be accessed without restriction by code which is defined outside of that member’s class. If you look back at last week’s lab notes you will see that this was the only type of class member that we used in any of the examples (this was implicit in our examples because the default in Java is for a member to be public). A private member can be accessed only by other methods which are defined by its class. It is through the use of private members that access is controlled in support of encapsulation. As we have mentioned in class, the restriction of access to a class’s members, is an important aspect of OOP because it helps to prevent the misuse of an object either maliciously or inadvertently. By allowing access to private data only through a well-defined set of methods, the assignment of improper values to that data can be prevented. It is not possible for code outside of the class to set the value of a private member directly. You, as the programmer/developer control precisely how and when the data within an object is used. When implemented properly, the class creates a “black box” that can be utilized to accomplish the task for which it was intended, but whose inner workings are not open to examination or tampering. COP 3503 – Java Notes #2 - 1 COP 3503 – Computer Science II – Java Notes #2 More Details on Classes and Methods
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Access Specifiers In Java, member access control is accomplished through the use of the access specifiers: public and private . [Note: there is actually a third one: protected , but will ignore this one for now as it only applies when inheritance is involved.] If no access specifier is explicitly listed, then the default specifier of public is utilized for a class member. An access specifier precedes the rest of a member’s type specification. Several examples below will illustrate the syntax of the access specifiers. Some access specifier examples
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 13

java2 - COP 3503 Computer Science II Java Notes#2 More...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online