1 / 11

Access Control to Class Members

Access Control to Class Members. In its support for encapsulation, the class provides two major benefits. First, it links data with the code that manipulates it. Second, it provides the means by which access to members can be controlled.

carsyn
Download Presentation

Access Control to Class Members

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Access Control to Class Members • In its support for encapsulation, the class provides two major benefits. • First, it links data with the code that manipulates it. • Second, it provides the means by which access to members can be controlled. • In essence, there are two basic types of class members: public and private. • A public member can be freely accessed by code defined outside of its class. tMyn

  2. A private member can be accessed only by other methods defined by its class. • It is through the use of private members that access is controlled. • Restricting access to a class’s members is a fundamental part of object-oriented programming because it helps prevent the misuse of an object. • By allowing access to private data only through a well-defined set of methods, you can prevent improper values from being assigned to that data – by performing a range check, for example. • It is not possible for code outside the class to set the value of a private member directly. tMyn

  3. You can also control precisely how and when the data within an object is used. • Thus, when correctly implemented, a class creates a “black box” that can be used, but the inner workings of which are not open to tampering. • The default setting is essentially public. • Member access control is achieved through the use of three access specifiers (access modifiers): public, private, and protected. • The protected access specifier applies only when inheritance is involved and it is described later. tMyn

  4. When a member of a class is modified by the public access specifier, that member can be accessed by any other code in your program wherever the program has a reference to an object of that class or one of its derived classes. • When a member of a class is specified as private, that member can be accessed only by other members of its class. Thus, methods in other classes cannot access a private member of another class. • The default access setting (in which no access specifier is used) is the same as public unless your program is broken down into packages. tMyn

  5. A package is, essentially, a grouping of classes. • Packages are both an organizational and access control feature, and those topics will be discussed later. • An access specifier must begin a member’s declaration statement. tMyn

  6. package TimoSoft; public class Student { private String firstName; private String lastName; public void getFirstName() { System.out.println("The first name is "+firstName+"."); } public void getLastName() { System.out.println("The last name is "+lastName+"."); } public void setFirstName(String fVal) { firstName=fVal; } tMyn

  7. public void setLastName(String lVal) { lastName=lVal; } public Student(String first, String last) { setFirstName(first); setLastName(last); } } tMyn

  8. Let’s modify the previous example: the instance variables will be asked from the user of the program: tMyn

  9. package TimoSoft; import java.util.Scanner; public class Student { private String firstName; private String lastName; public void getFirstName() { System.out.println("The first name is "+firstName+"."); } public void getLastName() { System.out.println("The last name is "+lastName+"."); } public void setFirstName() { Scanner fromKeyboard=new Scanner(System.in); System.out.print("Enter the first name, please: "); firstName=fromKeyboard.nextLine(); } tMyn

  10. public void setLastName() { Scanner fromKeyboard=new Scanner(System.in); System.out.print("Enter the last name, please: "); lastName=fromKeyboard.nextLine(); } public Student() { setFirstName(); setLastName(); } } tMyn

  11. package TimoSoft; public class StudentTest { public static void main(String[] args) { Student first=new Student(); first.getFirstName(); first.getLastName(); } } run: Enter the first name, please: Susan "YouTube" Enter the last name, please: Boyle The first name is Susan "YouTube". The last name is Boyle. BUILD SUCCESSFUL (total time: 23 seconds) tMyn

More Related