Pemrograman Berorientasi Object Interface IMade Suartana, S.Kom, M.Kom
Materi Interface class Final class Encapsulasi/information i hiding
Section 1 INTERFACE
Intro Dalam pemrograman Berorientasi Objek yang terpenting adalah apa yang dapat dilakukan sebuah objek, bukan bagaimanaobjek melakukan sesuatu. Pengguna hanya perlu tahu apa yang bisa dilakukan sebuah program, bukan bagaimana sebuah program mengerjakan suatu instruksi.
Interface Interface Interface merupakan komponen dalam vital dalam pemrograman berorientasi objek. Suatu interface merupakan suatu kontrak atau kesepakatan mengenai apa yang bisa dikerjakan oleh sebuah objek.
Contoh Interface
Modifier Interface 2 In the Java programming language, an interface is a reference type, similar to a class, that can contain only constants, methodsignatures signatures, and nested types. There are no method bodies. Interfaces cannotbe instantiated theycanthey onlybe implemented by classes or extended by other interfaces. http://docs.oracle.com/javase/tutorial/java/iandi/createinterface.html
Fungsi Interface There are a number of situations in software engineering when it is important for disparate groups of programmers to agree to a "contract" that spells out how their software interacts. Each group should be able to write their code without any knowledge of how the other group's code is written. Generally speaking, interfaces are such contracts. http://docs.oracle.com/javase/tutorial/java/iandi/createinterface.html
FungsiInterface 2 2 For example, imagine a futuristic society where computer controlled robotic cars transport passengers through city streets without a human operator. Automobile manufacturers write software that operates the automobile stop, start, accelerate, turn left, and so forth. Another industrial i group, electronic guidance instrument t manufacturers, make computer systems that receive GPS (Global Positioning System) )position data and wireless transmission of traffic conditions and use that information to drive the car. http://docs.oracle.com/javase/tutorial/java/iandi/createinterface.html
FungsiInterface 3 3 The auto manufacturers must publish an industry standard interface that spells out in detail what methods can be invoked to make the car move (any car, from any manufacturer). The guidance manufacturers can then write software that invokes the methods described in the interface to command the car. Neither industrial group needs to know how the other group's software is implemented. In fact, each group considers its software highly yproprietary p and reserves the right to modify it at any time, as long as it continues to adhere to the published interface. http://docs.oracle.com/javase/tutorial/java/iandi/createinterface.html
Interface & MultipleInheritance Java tidak mendukung multiple inheritance Interface merupakan alternati menerapkan konsep multiple inheritancepada java
Ilustrasi 2 2 Media Player Play() CD DVD Play() Play() Combo???
Solusi Solusi Interface
Deklarasi interface Deklarasi interface terdiri dari modifiers, kata kunci(keyword) interface, nama interface, super/parent dari interface(kalau ada) ditulis dengan pemisah (,) kalau lebih dari satu parent, body interface:
Contoh 2 Contoh
Deklarasi interface 2 2 Modifier interface dibuat public agar interface bisa diakses dari class dan package dari mana saja. Interface bisa diturunkan dari interfaces lain, sama seperti class yang bisa diturunkan dari class lain. li Hanya saja interface bisa diturunkan dari satu atau lebih interface lain ( sedangkan class hanya bisa diturunkan dari satu class lain). Tanda (,) digunakan sebagai pemisah kalau interface diturunkan lebih dari satu interface.
Interface Body Interface body terdiri dari deklarasi as semua method yang dibutuhkan dalam interface. Method pada interface tidak boleh memiliki body/implementasi dari method dan modifier dari method bersifat public. Interface bisa memiliki atribut constant (konstanta). All constant tvalues dfi defined din an interface are implicitly public, static, and final. Once again, these modifiers can be omitted.
Modifier 3 Implementasi Interface Untuk menggunakan/meng implementasikan interface dalam class, dengan menambahkan keyword implements setelah namaclass diikuti nama interface
Modifier 4 Point penting Interface Class yang mengimplementasikan class interface harus mengimplementasikan method yang dideklarasikan pada class interface(override) Semua method pada class interface adalah method public dan abstract Class interface tidakbisadi instantmenjadiobjek Instance dari class interface melalui lliimplementasi idari class interface Class interface bisa diturunkan(extend) dari satu atau Beberapa class interfaces. Sebuah class hanya bisa diturunkan dari satu class tetapi bisa meng implementasikan beberapainterface
Constructor Perbedaan penggunaan abstractvs interface Kelas abstrak digunakan ketika perlu mendefinisikan template untuk digunakan sekelompok kl ksubclass bl Interface digunakan jika sebuah peran diperlukan untuk kelas lain, terlepas dari konsep penurunan(inheritance)
Section 2 FINAL CLASS DAN METHOD
Modifier 5 Final class dan method Dideklarasikan menggunakan keyword final Final class tidak bisa diturunkan(extend). Final method tidak bisa di override id
Modifier 5 contoh Final class Final Method
Section 3 ENKAPSULASI & INFORMATION HIDING
Modifier 5 Enkapsulasi & Information Hiding Information hiding adalah penyembunyian attribut suatu objek dari objek lain. Enkapsulasi adalah penyembunyian method suatu objek dari objek lain Set modifier menjadi private
Latihan