Modul 2 PBO I Konsep Dasar Berorientasi Obyek
Modul 2 PBO I Konsep Dasar Berorientasi Obyek
Karakteristik Pemrograman Berorientasi Objek: Enkapsulasi (Pengkapsulan) u Pewarisan u Reusability (Penggunaan Kembali) u Spesialisasi u Generalisasi u Polymorfisme (Banyak Bentuk) u Komunikasi antar objek u
Bahasa Pemrograman yang mendukung PBO : u Java u Eiffel u C++ u Small. Talk u Python u (Delphi)
Sekilas Bahasa Java //Contoh program Sederhana class coba { public static void main(String args[]) { System. out. println("I'm a Simple Program"); } }
Enkapsulasi u Dapat dikatakan sebagai penyembunyian data/informasi (Information hiding). u Implementasi ke bahasa Pemrograman (Java, C++) dengan menggunakan class.
Enkapsulasi Dalam kehidupan sehari-hari, penyembunyian data sudah sering kita alami. Contohnya adalah objek Handphone; dimana setiap orang yang menggunakannya tidak mengetahui secara pasti apa yang ada di dalam Handphone tersebut. Tidak mengetahui bagaimana cara kerja HP tersebut. Yang penting adalah menggunakannya melalui interface yang disediakan.
Contoh Enkapsulasi
Objek dlm dunia nyata Sesuatu yang mempunyai status, kelakuan, dan interface. Contoh dalam dunia nyata adalah Mobil. Objek Mobil mempunyai : u status : jenis, warna, plat_nomor u kelakuan : jalan_maju, jalan_mundur u interface : kemudi, rem, pedal_gas, tuas
Contoh Objek Dunia Nyata u u Semua mobil mempunyai interface yang mirip, walaupun masing-masing mobil mempunyai mekanisme dan cara kerja yang berbeda. Sepanjang kita tahu interface standar mobil; yaitu kemudi, pedal gas, rem, tuas persneling, maka kita dapat mengemudikannya. Kita mungkin tidak tahu cara kerja dan jenis mesin yang menggerakkan mobil tersebut. Ada beberapa jenis mobil yang berbeda, dengan mesin yang berbeda-beda, tetapi menggunakan interface yang sama. Sehingga pengguna mobil dapat menggunakannya.
Objek dalam software Pada software, objek juga mempunyai status, kelakuan, dan interface. u Status adalah variabel. u Kelakuan adalah method; yaitu fungsi (subroutine) yang berhubungan dengan objek. u Interface adalah kumpulan method yang akan digunakan untuk memanipulasi dan berinteraksi dengan objek.
Pengertian class u Implementasi dari Information Hiding; yaitu kumpulan dari berbagai tipe variabel maupun method yang setiap komponen software “melihatnya” u Merupakan blue print dari objek yang akan diciptakan. u Mendiskripsikan interface objek, struktur status objek, dan method objek secara menyeluruh.
class mempunyai : u Atribut/Variabel. Nilai atribut pada saat runtime menyatakan status/variabel objek dari instantiation class tersebut. u Method. Pada saat runtime, method akan dieksekusi sesuai dengan kode programnya atas permintaan objek lain. u Interface. Merupakan kumpulan Method-method yang ada dalam class , yang dapat digunakan untuk memanipulasi objek.
class dan objek u Objek merupakan instance dari class. u Proses penciptaan objek disebut dengan instantiation.
Syntax class nama. Kelas { attribut; //variabel instance nama. Kelas() {} //constructor void method_1() { } void method_2() { } : }
Contoh class Mobil { String warna; int kursi; Mobil() {} //constructor void jalan_maju() { } void jalan_mundur() { } }
Pendeklarasian objek public static void main(String[] args) { Mobil sedan = new Mobil(); instantiation sedan. jalan_maju(); } pemanggilan objek
Pendeklarasian class dan Objek class Mobil { Mobil() {} //constructor void jalan_maju() { System. out. println("Maju … !"); } public static void main(String[] args) { Mobil sedan = new Mobil(); sedan. jalan_maju(); } }
Perbedaan class dan objek class adalah deskripsi statik dari objek yang diciptakan. Objek di sini merupakan instantiation dari class. u objek adalah elemen yang pada saat runtime diciptakan, dimanipulasi, dan dihancurkan. Secara umum dapat dikatakan bahwa pada saat runtime, yang kita punyai adalah objek. Di dalam teks program, yang kita punyai adalah class. •
Lingkup class u Private. Feature yang “visible” hanya untuk class dimana feature tersebut didefinisikan. u Protected. Feature yang disembunyikan terhadap class client, tetapi “visible” untuk class turunan. u Public. Feature yang “visible”, dapat dipakai oleh semua class yang ada (client, turunan).
Lingkup class (Istilah) u Feature disini adalah Atribut dan/atau Method. u Visible artinya dapat dilihat atau dapat dipanggil dari class client. u Client artinya class - class yang lainnya, yang tidak berhubungan dengan class yang dimaksud.
Pengertian Method u Dalam bahasa Pemrograman yang lain sering disebut dengan Procedure atau Function, lengkap dengan signature-nya. Signature disini adalah nama dan parameter. u Method merupakan encapsulation kode program. u Method terdiri dari urutan statement/kode program yang dieksekusi ketika Method dipanggil.
Gambaran Method void main() { statement; fungsi. Ku_1() ; : : statement; : fungsi. Ku_2() ; : statement; fungsi. Ku_4() ; : return 0; } fungsi. Ku_1 ------return fungsi. Ku_2 ---fungsi. Ku_3() ; ---return fungsi. Ku_4 -----return fungsi. Ku_3 -------return
Contoh Method class Mobil { String warna; int kursi; interface void jalan_maju() { System. out. println(“Maju”); { Method void jalan_mundur() { System. out. println(“Mundur”); } }
- Slides: 23