Presentation is loading. Please wait.

Presentation is loading. Please wait.

DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA ‏ Visibility Control.

Similar presentations


Presentation on theme: "DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA ‏ Visibility Control."— Presentation transcript:

1 DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA ‏ Visibility Control

2 Usually the variables and methods of a class are visible everywhere in the program. However, in some situations, it may be necessary to restrict the access to certain variables and methods from outside the class. For example it may need to restrict the update of the balance field of a bank account object directly for other classes or programs. This can be achieved in Java by applying access modifiers. Java provides for types of access modifiers. 1. public. 2. default. 3. protected. 4. private.. 2

3 Syntax: for variables: ; E.g.: public int num; private double balance; for methods: ( ) { //body of the method. } E.g.: public double add(int a,int b) { return a+b; } 3

4 Public Access Any variable or method declared as public has the widest possible visibility and they are accessible everywhere. Note that the main method of a Java application is declared as public so that any class can access it without any restriction because it is the starting point of the program.. 4

5 Default Access For the default access of a variable or a method no access modifiers are used. This is also known as the friendly access. The difference between the public access and the friendly access is that the public modifier make the fields visible in all classes, regardless of their packages while the friendly access makes fields visible only in the same package, but not in other packages. (Note: package is a group of related classes stored separately). 5

6 Protected Access The visibility level of a “Protected” field lies in between the public access and default access. That is, the protected modifier makes the fields visible only to all classes and subclasses in the same package but also to subclasses in other packages. Note that, the non-subclasses in other packages cannot access the “protected” members 6

7 Private Access private fields have the highest degree of protection. They are accessible only within their own class. A method declared as private behaves like a method declared as final. This prevents the method from being sub classed. 7

8 summarizes the visibility 8

9 Instance and methods of class Usually the variables of a class are declared as private to provide the highest protection and the methods are declared as public so that they can be accessed by other classes. Eg:-in Student class 9 private String name; private int age; private double marks; private char grade; public void display()

10 Instance and methods of class Within driver class if you access marks directly i.e. st1.marks=78; This will produce the following compile time “marks has private access in Student” This is because the fields declared as private can not be accessed out side its class. Now to set the marks of a particular student a public method should be included into the Student class. Similarly if it is necessary to get the value of the marks field another public method should facilitate. 10

11 Setters and Getters Setters and Getters are two types of public methods which are used to set and get the value of an attribute from outside of its class. Naming Conventions: The name of the method starts with the word “set” or “get” followed by the name of the attribute that the method can manipulate. The Java method naming convention is also applied (if more than two words first letter of the first word is lowercase and first letter of other words are uppercase) 11

12 Setters and Getters… Ex: To set a value to the attribute name a setter method is defined as follows public void setName(String n) { this.name=n; } To get the value of the name attribute can use a getter method. public String getName() { return this.name; } 12


Download ppt "DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA ‏ Visibility Control."

Similar presentations


Ads by Google