OOP: Encapsulation

Run Settings
LanguageJava
Language Version
Run Command
class Main { public static void main(String[] args) { System.out.println("Contoh Encapsulation di Java"); System.out.println("----------------------------"); Mahasiswa mhs = new Mahasiswa(); // mhs.nim = "112233"; error jika diakses secara langsung // mhs.nama = "Fulan bin Fulan" mhs.setNim("112233"); mhs.setNama("Fulan bin Fulan"); System.out.println("NIM : "+ mhs.getNim()); System.out.println("Nama : "+ mhs.getNama()); } }
class Mahasiswa { // Properti nim dan nama 'dibungkus' (kapsul) sehingga // Nim dan Nama tidak bisa diakses secara langsung private String nim, nama; public void setNim(String nim) { this.nim = nim; } public void setNama(String nama) { this.nama = nama; } public String getNim() { return nim; } public String getNama() { return nama; } }
Editor Settings
Theme
Key bindings
Full width
Lines