Lecture 6
Lecture 6
Introduction to java
Lecture 6
Kandahar University
Faculty of Computer Science
Niaz.M "doostyar"
1
+ 2
Today’s Lecture
Java Package
Access Modifiers
Encapsulation
Question
Niaz.M "doostyar"
+ 3
Creating Package
assume
Niaz.M "doostyar"
+ 4
Niaz.M "doostyar"
5
Niaz.M "doostyar"
6
Niaz.M "doostyar"
+ 7
Niaz.M "doostyar"
+ 8
Compiling TestA
Niaz.M "doostyar"
+ 9
Right Way
Niaz.M "doostyar"
+ 10
Niaz.M "doostyar"
+ 11
Lets Compile
Niaz.M "doostyar"
+ 12
Niaz.M "doostyar"
+ 13
Niaz.M "doostyar"
14
Niaz.M "doostyar"
15
Niaz.M "doostyar"
+ 16
Access Specifies
default
private
protected
public
Niaz.M "doostyar"
+ 17
Niaz.M "doostyar"
18
Niaz.M "doostyar"
19
Niaz.M "doostyar"
20
Niaz.M "doostyar"
21
Niaz.M "doostyar"
22
Niaz.M "doostyar"
23
Niaz.M "doostyar"
24
Niaz.M "doostyar"
25
Niaz.M "doostyar"
26
Niaz.M "doostyar"
+ 27
Encapsulation
encapsulation in java
We can create a fully encapsulated class in Java by making all the
data members of the class private. Now we can use setter and
getter methods to set and get the data in it.
Niaz.M "doostyar"
+ 28
Advantage of Encapsulation
By providing only a setter or getter method, you can make
the class read-only or write-only. In other words, you can
skip the getter or setter methods.
It provides you the control over the data. Suppose you want
to set the value of id which should be greater than 100 only,
you can write the logic inside the setter method. You can
write the logic not to store the negative numbers in the setter
methods.
Read-Only class
Niaz.M "doostyar"
+ 30
Write-Only class
Niaz.M "doostyar"
31
Niaz.M "doostyar"
32
Niaz.M "doostyar"