Blogroll

Senin, 02 Juli 2012

Enkapsulasi

Posted by Unknown On 18.14 No comments

Enkapsulasi adalah salah satu dari empat konsep OOP mendasar. The other three are inheritance, polymorphism, and abstraction. Tiga lainnya adalah pewarisan, polimorfisme, dan abstraksi.
Encapsulation is the technique of making the fields in a class private and providing access to the fields via public methods. Enkapsulasi adalah teknik pembuatan ladang di kelas swasta dan menyediakan akses ke ladang melalui metode publik. If a field is declared private, it cannot be accessed by anyone outside the class, thereby hiding the fields within the class. Jika bidang ini dideklarasikan swasta, tidak dapat diakses oleh siapa pun di luar kelas, sehingga menyembunyikan bidang dalam kelas. For this reason, encapsulation is also referred to as data hiding. Untuk alasan ini, enkapsulasi juga disebut sebagai persembunyian data.
Encapsulation can be described as a protective barrier that prevents the code and data being randomly accessed by other code defined outside the class. Enkapsulasi dapat digambarkan sebagai lapisan pelindung yang mencegah kode dan data yang secara acak diakses oleh kode lain didefinisikan di luar kelas. Access to the data and code is tightly controlled by an interface. Akses ke data dan kode yang dikontrol ketat oleh interface.
The main benefit of encapsulation is the ability to modify our implemented code without breaking the code of others who use our code. Manfaat utama dari enkapsulasi adalah kemampuan untuk mengubah kode kita dilaksanakan tanpa melanggar kode orang lain yang menggunakan kode kita. With this feature Encapsulation gives maintainability, flexibility and extensibility to our code. Dengan fitur Enkapsulasi memberikan rawatan, fleksibilitas dan diperpanjang untuk kode kita.
Example: Contoh:
Let us look at an example that depicts encapsulation: Mari kita lihat contoh yang menggambarkan enkapsulasi:
/* File name : EncapTest.java */ / * File name: EncapTest.java * /
public class EncapTest{ public class EncapTest {

   private String name; swasta String nama;
   private String idNum; swasta String idNum;
   private int age; swasta int umur;

   public int getAge(){ publik getAge int () {
      return age; kembali usia;
   } }

   public String getName(){ public String getName () {
      return name; kembali nama;
   } }

   public String getIdNum(){ public String getIdNum () {
      return idNum; kembali idNum;
   } }

   public void setAge( int newAge){ public void setAge (int NewAge) {
      age = newAge; umur = NewAge;
   } }

   public void setName(String newName){ public void setName (String newname) {
      name = newName; name = newname;
   } }

   public void setIdNum( String newId){ public void setIdNum (String newid) {
      idNum = newId; idNum = newid;
   } }
} }
The public methods are the access points to this class.s fields from the outside java world. Metode akses publik poin untuk bidang ini class.s dari dunia java luar. Normally these methods are referred as getters and setters. Biasanya metode ini disebut sebagai getter dan setter. Therefore any class that wants to access the variables should access them through these getters and setters. Oleh karena itu setiap kelas yang ingin mengakses variabel harus mengaksesnya melalui getter dan setter.
The variables of the EncapTest class can be access as below:: Variabel kelas EncapTest dapat diakses sebagai berikut ::
/* File name : RunEncap.java */ / * File name: RunEncap.java * /
public class RunEncap{ public class RunEncap {

   public static void main(String args[]){ public void static main (String args []) {
      EncapTest encap = new EncapTest(); EncapTest encap = new EncapTest ();
      encap.setName("James"); encap.setName ("James");
      encap.setAge(20); encap.setAge (20);
      encap.setIdNum("12343ms"); encap.setIdNum ("12343ms");

      System.out.print("Name : " + encap.getName()+ System.out.print ("Nama:" + encap.getName () +
                             " Age : "+ encap.getAge()); "Umur:" + encap.getAge ());
    } }
} }
This would produce following result: Hal ini akan menghasilkan hasil sebagai berikut:
Name : James Age : 20 Nama: James Umur: 20
Benefits of Encapsulation: Manfaat Enkapsulasi:
  • The fields of a class can be made read-only or write-only. Bidang kelas dapat dibuat hanya-baca atau menulis saja.
  • A class can have total control over what is stored in its fields. Kelas A dapat memiliki total kontrol atas apa yang disimpan dalam bidangnya.
  • The users of a class do not know how the class stores its data. Para pengguna dari kelas tidak tahu bagaimana kelas menyimpan 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. Kelas A dapat mengubah tipe data dari field, dan pengguna kelas tidak perlu mengubah salah satu kode mereka.

0 komentar:

Posting Komentar

Site search

    Blogger news

    Blogroll

    About