Home > Encapsulation, Java Programming > Encapsulation

Encapsulation

adalah suatu cara untuk menyembunyikan implementasi detail dari suatu class.
Enkapsulasi mempunyai dua hal mendasar, yaitu:

  • information hiding
  • interface to access data

Contoh :

Kita buat New Class Siswa

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package encapsulation;

/**
*
* @author Arin
*/
public class Siswa {

public String nama;
public String nrp;

public void Info() {
System.out.println(“Saya adalah”);
System.out.println(“Nama ” + nama);
System.out.println(“Nrp ” + nrp);
}
}

Setelah itu kita buat New Class lagi dengan nama IsiData

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package encapsulation;

/**
*
* @author Arin
*/
public class IsiData {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Siswa IT = new Siswa();
IT.nama = “Agus”;
IT.nrp = “2006420052″;
IT.Info();

}
}

Running program Isi data dan coba liat hasil Running tersebut.

Advertisement
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.