Presentasi sedang didownload. Silahkan tunggu

Presentasi sedang didownload. Silahkan tunggu

Enkapsulasi.

Presentasi serupa


Presentasi berjudul: "Enkapsulasi."— Transcript presentasi:

1 Enkapsulasi

2 Enkapsulasi ?? States class artis { String nama; Nama String alamat;
int umur; } Nama Alamat Umur Apa mungkin?? Tipe data?? artis artis1 = new artis(); artis1.nama = “Dian Sastro”; artis1.alamat = “Jln Kalimantan 37 Jember”; artis1.umur = 231; artis1.umur = 25.12;

3 Enkapsulasi?? class artis { String nama; String alamat; int umur; }
Tdk dpt diakses scr langsung class artis { private String nama; private String alamat; private int umur; public void setUmur (int umur){ this.umur = umur } public int getUmur(){ return this.umur; Hrs diakses melalui method

4 Enkapsulasi CLASS Variabel Instan Tdk dapat diakses dari luar kelas
Method Dapat diakses dari luar kelas

5 Enkapsulasi is the technique of making the fields in a class private and providing access to the fields via public methods can be described as a protective barrier that prevents the code and data being randomly accessed by other code defined outside the class field is declared private, it cannot be accessed by anyone outside the class, thereby hiding the fields within the class. For this reason, encapsulation is also referred to as data hiding Using “private” keyword (yg dikenal dng access modifier)

6 Why Enkapsulasi?? Benefits of Encapsulation:
The fields of a class can be controlled whether it will be read-only or write-only. class person{ private String Nama; private String Pekerjaan; public person(String Pekerjaan){ this.Pekerjaan = Pekerjaan; } public String getPekerjaan(){ return this.Pekerjaan; Tidak dpt diubah (read only) person person1 = new person(“Direktur”); person person2 = new person(“OB”); person1 person2

7 Why Enkapsulasi?? Benefits of Encapsulation:
A class can have total control over what is stored in its fields.

8 Why Enkapsulasi?? Benefits of Encapsulation:
The users of a class do not have to know how the class stores its data. A class can change the data type of a field, and users of the class do not need to change any of their code.

9 Private Variabel & Method Public Variabel & Method
Private vs Public CLASS Private Variabel & Method Access Modifier: privete Tdk dapat diakses dari luar kelas Public Variabel & Method Access Modifier: public Dapat diakses dari luar kelas

10 Private vs. Public class buah{ private double hargaBuah;
public String warnaBuah; } Dari kelas lain → main() buah mangga = new buah(();

11 Tanpa Access Modifier? Dapat diakses dari seluruh kelas class buah{
private double hargaBuah; public String warnaBuah; String bentukBuah; } Dari kelas lain → main() buah mangga = new buah(();

12 Tanpa Access Modifier? Tidak dapat diakses dari luar package

13 Protected Variabel & Method
Hanya dapat diakses oleh subclassnya (meski dr package lain), TETAPI tdk dpt diakses oleh client kelas secara langsung. Package 1 Package 2 CLASS A SUBCLASS Protected Variabel & Method CLASS D CLASS B CLASS E

14 Protected

15 Thx….


Download ppt "Enkapsulasi."

Presentasi serupa


Iklan oleh Google