Presentation is loading. Please wait.

Presentation is loading. Please wait.

Access Control to Class Members tMyn1 Access Control to Class Members In its support for encapsulation, the class provides two major benefits. First, it.

Similar presentations


Presentation on theme: "Access Control to Class Members tMyn1 Access Control to Class Members In its support for encapsulation, the class provides two major benefits. First, it."— Presentation transcript:

1 Access Control to Class Members tMyn1 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.

2 Access Control to Class Members tMyn2 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.

3 Access Control to Class Members tMyn3 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.

4 Access Control to Class Members tMyn4 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.

5 Access Control to Class Members tMyn5 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.

6 Access Control to Class Members tMyn6 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; }

7 Access Control to Class Members tMyn7 public void setLastName(String lVal) { lastName=lVal; } public Student(String first, String last) { setFirstName(first); setLastName(last); }

8 Access Control to Class Members tMyn8 Let’s modify the previous example: the instance variables will be asked from the user of the program:

9 Access Control to Class Members tMyn9 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(); }

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

11 Access Control to Class Members tMyn11 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)


Download ppt "Access Control to Class Members tMyn1 Access Control to Class Members In its support for encapsulation, the class provides two major benefits. First, it."

Similar presentations


Ads by Google