0% found this document useful (0 votes)
15 views

Lecture 6

The document discusses a lecture on Java packages, access modifiers, and encapsulation. It covers how to create packages, add classes to packages, compile classes in packages, and the access rules for different access modifiers like public, private, and protected. It also defines encapsulation as wrapping code and data together, and provides examples of read-only and write-only classes to illustrate encapsulation principles in Java.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Lecture 6

The document discusses a lecture on Java packages, access modifiers, and encapsulation. It covers how to create packages, add classes to packages, compile classes in packages, and the access rules for different access modifiers like public, private, and protected. It also defines encapsulation as wrapping code and data together, and provides examples of read-only and write-only classes to illustrate encapsulation principles in Java.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

+

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

Adding class in test package

Niaz.M "doostyar"
5

Niaz.M "doostyar"
6

Niaz.M "doostyar"
+ 7

TestA class in test package

Niaz.M "doostyar"
+ 8

Compiling TestA

Niaz.M "doostyar"
+ 9

Right Way

Niaz.M "doostyar"
+ 10

Lets Shift TestA to D:\

Niaz.M "doostyar"
+ 11

Lets Compile

Niaz.M "doostyar"
+ 12

Using classpath switch

Niaz.M "doostyar"
+ 13

Solution -> Error

Niaz.M "doostyar"
14

Niaz.M "doostyar"
15

Niaz.M "doostyar"
+ 16

Access Specifies

 default
 private
 protected
 public

Niaz.M "doostyar"
+ 17

Simple Rule Table

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 is a process of wrapping code and data


together into a single unit, for example, a capsule which is
mixed of several medicines.

 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.

 The Java Bean class is the example of a fully encapsulated


class.

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.

 It is a way to achieve data hiding in Java because other class


will not be able to access the data through the private data
members.

 The encapsulate class is easy to test. So, it is better for unit


testing.
Niaz.M "doostyar"
+ 29

Read-Only class

 //A Java class which has only getter methods.


 public class Student{
//private data member
private String college="AKG";
//getter method for college
public String getCollege(){
return college;
}
 }

Niaz.M "doostyar"
+ 30

Write-Only class

 //A Java class which has only setter methods.


 public class Student{
//private data member
private String college;
//getter method for college
public void setCollege(String college){
this.college=college;
}
 }

Niaz.M "doostyar"
31

Niaz.M "doostyar"
32

Niaz.M "doostyar"

You might also like