SlideShare a Scribd company logo
MEMAHAMI INTERFACE DI
JAVA (DAN CONTOHNYA)
Oleh: Saprudin Komala, S.Kom.
• Ada yang bilang gini:
• “Interface adalah blue print dari class. Isi method-nya kosong dan nanti akan
diimplementasikan pada class lain.”
• Kamu bisa paham maksudnya?
• Tidak?
• Sama.. hehe 😄
• Ya, seperti itulah penjelasan yang saya terima saat baru awal belajar Java dan
OOP dulu. Bukan tambah paham, malah bikin bingung.
• Interface memang tak memiliki isi method dan pada tutorial sebelumnya kita
juga pakai interface untuk beberapa program java.
• Tapi bukan itu sebenarnya kegunaan utama dari Interface.
• Lalu apa donk?
• Mari kita bahas.
APA ITU INTERFACE?
• Interface terdiri dari dua kata:
• Inter = antar
• Face = muka
• Jadi interface adalah antarmuka.
• Kata ini mungkin sering kamu dengar. Contohnya seperti: GUI (Graphical User Interface) dan CLI
(Command Line Interface).
• Secara umum, interface berfungsi sebagai penghubung antara sesuatu yang ‘abstrak’ dengan sesuatu
yang nyata.
• Contoh:
• Coba perhatikan handphone-mu, ada berapa tombol di sana?
• Kalau HP zaman sekarang, paling cuma ada dua.. yakni tombol power dan tombol volume.
• Tombol-tombol inilah yang dimaksud interface. Sementara isi dalam HP-nya, kamu tak peduli atau tak
akan tahu seberapa kompleks dan abstrak sistem yang ada di sana.
• Saat kamu menekan tombol di HP mu, ada beberapa hal yang mungkin terjadi:
• HP menyala;
• HP mati;
• Suara HP tambah besar;
• Suara HP mengecil;
• Mengambil gambar dari kamera; dll.
• Ini tergantung bagaimana implementasi yang diberikan untuk tombol-tombol tersebut.
• Beda merek HP, beda lagi cara mereka mengimplementasikannya.
• Misal pada HP tertentu, jika menekan tombol volume 3x.. maka akan mengambil screenshot.
Sementara di HP merek lain, kita tidak bisa melakukan ini.
• Nah, konsep ini juga ada di dalam OOP.
• Bagaimana Contohnya?
• Mari kita lihat:
CONTOH PROGRAM INTERFACE
• Biar nyambung dengan penjelasan saya yang tadi, kita akan pakai contoh
Handphone.
• Jadi.. ada dua objek yang akan kita hubungkan dengan interface.
• Yakni manusia dan handphone. Manusianya kita sebut saja sebagai PhoneUser.
Karena dia akan menggunakan objek HP
.
• Maka dapat kita buat class diagramnya seperti ini:
• Sekarang mari kita coba buat dalam bentuk kode. Buatlah proyek baru dengan
nama ContohInterface di Netbeans atau text editor apapun.
• Lanjutan dari atas:
• Lanjutan dari atas:
• Sekarang coba jalankan class Main. Caranya, Klik kanan lalu pilih Run File atau bisa
juga dengan menekan Shift+F6.
• Maka hasilnya:
• Kalau kita perhatikan di dalam method main() kita hanya menggunakan objek dari
class PhoneUser saja.
• Coba lihat kode-kode ini:
• Saat kita memanggil method-method ini, maka method yang ada di dalam objek
redmiNote8 yang akan dipanggil.
• Ini karena mereka terhubung dengan interface.
• Lalu pertanyaannya:
MENGAPA HARUS PAKAI
INTERFACE?
• Sebenarnya kita tidak harus selalu menggunakan interface. Tapi dalam kasus
tertentu, interface akan sangat berguna.
• Misalnya gini:
• Kita tahu objek PhoneUser membutuhkan objek dari handphone. Tanpa objek
handphone, si objek user nggak akan bisa kita buat.
• Ini karena kita menetapkan pada konstruktornya.
• Btw, ini juga disebut dengan hubungan dependency.
• Nah, di sana kita menggunakan interface Phone untuk membuat objek
dependency-nya.
• Kalau gak pakai interface memangnya kenapa?
• Untuk menjawab pertanyaan ini, mari kita coba ubah Phone menjadi Xiaomi.
• Apa yang akan terjadi?
• Untuk saat ini tidak akan terjadi apa-apa. Tapi sekarang bagaimana kalau kita punya
objek baru dari class yang mengimplementasikan interface Phone juga.
• Kita sebut saja objek baru tersebut adalah iPhoneX yang merupakan instance dari
class IPhone.
• Maka object iPhoneX
tidak akan bisa diberikan
kepada PhoneUser. Karena
di konstruktornya dia cuma
bisa pakai Xiaomi saja.
Jadi.. Mengapa kita harus
pakai interface?
Interface akan membuat si objek PhoneUser bisa menggunaakn objek hand-phone
apapun dengan syarat objek handphone tersebut harus mengimple-mentasikan
method dari interface Phone. Jelas?
HAL-HAL YANG TIDAK BOLEH
DILAKUKAN DI INTERFACE
• Ada beberapa hal yang tidak boleh kamu lakukan saat membuat interface:
• Jangan buat variabel di dalam interface, tapi membuat konstanta boleh;
• Jangan mengisi method-nya, cukup tuliskan nama method, tipe data, dan
parameter saja. Tapi untuk default metod boleh punya isi.
• Contoh:
• Jangan berikan modifier privete atauapun protected pada method dan konstanta
yang ada di dalam interface.
• Interface tidak bisa dibuat objek instance-nya dengan kata kunci new.
• Kalau dilanggar gimana?
• Ya programnya akan error!
APA SELANJUTNYA?
• Jadi kesimpulannya:
• Interface merupakan penghubung antar objek. Interface bersifat abstrak, sehingga
objek yang menggunakannya tidak akan perduli bagaimana ia diimplementasikan.
• Karena bersifat abstrak, interface tidak bisa dibuat objek instance dengan kata
kunci new.
• Interface sebenarnya mengamalkan prinsip Abstraksi dan Enkapsulasi
(pembungkusan).
• Nah, itu saja yang bisa saya bagikan tentang interface.
• Selanjutnya, silahkan kalian praktekkan program interface di atas.
• Sumber referensi: https://www.petanikode.com/java-oop-interface/
SEKIAN DAN TERIMA KASIH

More Related Content

Similar to 13. memahami interface di java (dan contohnya)

Ubercool, pixel perfct & slick design… that just doesn't work
Ubercool, pixel perfct & slick design… that just doesn't workUbercool, pixel perfct & slick design… that just doesn't work
Ubercool, pixel perfct & slick design… that just doesn't work
Samuel Bednar
 
Did i do the right thing show
Did i do the right thing showDid i do the right thing show
Did i do the right thing showFajri Abdillah
 
Monad Fact #6
Monad Fact #6Monad Fact #6
Monad Fact #6
Philip Schwarz
 
Framer for the win - Using Framer prototypes for your app project. (For Melbo...
Framer for the win - Using Framer prototypes for your app project. (For Melbo...Framer for the win - Using Framer prototypes for your app project. (For Melbo...
Framer for the win - Using Framer prototypes for your app project. (For Melbo...
Jinju Jang
 
Geecon10: Object Oriented for nonbelievers
Geecon10: Object Oriented for nonbelieversGeecon10: Object Oriented for nonbelievers
Geecon10: Object Oriented for nonbelievers
Bruno Bossola
 
Shortcut in learning iOS
Shortcut in learning iOSShortcut in learning iOS
Shortcut in learning iOS
Joey Rigor
 
Application development with Python - Desktop application
Application development with Python - Desktop applicationApplication development with Python - Desktop application
Application development with Python - Desktop application
Bao Long Nguyen Dang
 
An update about 3D Touch - What is it and what can we do with it?
An update  about 3D Touch - What is it and what can we do with it?An update  about 3D Touch - What is it and what can we do with it?
An update about 3D Touch - What is it and what can we do with it?
Soda studio
 
Tl bot
Tl botTl bot
On Readability of Code
On Readability of CodeOn Readability of Code
On Readability of Code
Arun Saha
 
Mastering python lesson2
Mastering python lesson2Mastering python lesson2
Mastering python lesson2
Ruth Marvin
 
Cis 1403 lab1- the process of programming
Cis 1403 lab1- the process of programmingCis 1403 lab1- the process of programming
Cis 1403 lab1- the process of programming
Hamad Odhabi
 
Automatic reference counting (arc) and memory management in swift
Automatic reference counting (arc) and memory management in swiftAutomatic reference counting (arc) and memory management in swift
Automatic reference counting (arc) and memory management in swift
InnovationM
 
Sw-3.Isde5
Sw-3.Isde5Sw-3.Isde5
Sw-3.Isde5s1170214
 
Creating a Facebook Clone - Part I - Transcript.pdf
Creating a Facebook Clone - Part I - Transcript.pdfCreating a Facebook Clone - Part I - Transcript.pdf
Creating a Facebook Clone - Part I - Transcript.pdf
ShaiAlmog1
 
UX 101: User Research methods to kickstart your project
UX 101: User Research methods to kickstart your projectUX 101: User Research methods to kickstart your project
UX 101: User Research methods to kickstart your project
Charlotte Breton Schreiner
 
Introducing small basic
Introducing small basicIntroducing small basic
Introducing small basicAn I
 

Similar to 13. memahami interface di java (dan contohnya) (20)

Week10
Week10Week10
Week10
 
Ubercool, pixel perfct & slick design… that just doesn't work
Ubercool, pixel perfct & slick design… that just doesn't workUbercool, pixel perfct & slick design… that just doesn't work
Ubercool, pixel perfct & slick design… that just doesn't work
 
Did i do the right thing show
Did i do the right thing showDid i do the right thing show
Did i do the right thing show
 
Monad Fact #6
Monad Fact #6Monad Fact #6
Monad Fact #6
 
Framer for the win - Using Framer prototypes for your app project. (For Melbo...
Framer for the win - Using Framer prototypes for your app project. (For Melbo...Framer for the win - Using Framer prototypes for your app project. (For Melbo...
Framer for the win - Using Framer prototypes for your app project. (For Melbo...
 
Geecon10: Object Oriented for nonbelievers
Geecon10: Object Oriented for nonbelieversGeecon10: Object Oriented for nonbelievers
Geecon10: Object Oriented for nonbelievers
 
Shortcut in learning iOS
Shortcut in learning iOSShortcut in learning iOS
Shortcut in learning iOS
 
Application development with Python - Desktop application
Application development with Python - Desktop applicationApplication development with Python - Desktop application
Application development with Python - Desktop application
 
An update about 3D Touch - What is it and what can we do with it?
An update  about 3D Touch - What is it and what can we do with it?An update  about 3D Touch - What is it and what can we do with it?
An update about 3D Touch - What is it and what can we do with it?
 
Tl bot
Tl botTl bot
Tl bot
 
On Readability of Code
On Readability of CodeOn Readability of Code
On Readability of Code
 
Mastering python lesson2
Mastering python lesson2Mastering python lesson2
Mastering python lesson2
 
Cis 1403 lab1- the process of programming
Cis 1403 lab1- the process of programmingCis 1403 lab1- the process of programming
Cis 1403 lab1- the process of programming
 
Notes
NotesNotes
Notes
 
Automatic reference counting (arc) and memory management in swift
Automatic reference counting (arc) and memory management in swiftAutomatic reference counting (arc) and memory management in swift
Automatic reference counting (arc) and memory management in swift
 
Sw-3.Isde5
Sw-3.Isde5Sw-3.Isde5
Sw-3.Isde5
 
Royf03
Royf03Royf03
Royf03
 
Creating a Facebook Clone - Part I - Transcript.pdf
Creating a Facebook Clone - Part I - Transcript.pdfCreating a Facebook Clone - Part I - Transcript.pdf
Creating a Facebook Clone - Part I - Transcript.pdf
 
UX 101: User Research methods to kickstart your project
UX 101: User Research methods to kickstart your projectUX 101: User Research methods to kickstart your project
UX 101: User Research methods to kickstart your project
 
Introducing small basic
Introducing small basicIntroducing small basic
Introducing small basic
 

More from Saprudin Eskom

soal + jawaban PAT DPPLG kelas X PPLG.docx
soal + jawaban PAT DPPLG kelas X PPLG.docxsoal + jawaban PAT DPPLG kelas X PPLG.docx
soal + jawaban PAT DPPLG kelas X PPLG.docx
Saprudin Eskom
 
Soal + jawaban PAT PBO Kelas XI RPL 2021.docx
Soal + jawaban PAT PBO Kelas XI RPL 2021.docxSoal + jawaban PAT PBO Kelas XI RPL 2021.docx
Soal + jawaban PAT PBO Kelas XI RPL 2021.docx
Saprudin Eskom
 
RPMS PAT PBO XI.docx
RPMS PAT PBO XI.docxRPMS PAT PBO XI.docx
RPMS PAT PBO XI.docx
Saprudin Eskom
 
RPMS PAT DPPLG X.docx
RPMS PAT DPPLG X.docxRPMS PAT DPPLG X.docx
RPMS PAT DPPLG X.docx
Saprudin Eskom
 
PAS Ganjil Digital Branding kelas XI Pemasaran
PAS Ganjil Digital Branding kelas XI PemasaranPAS Ganjil Digital Branding kelas XI Pemasaran
PAS Ganjil Digital Branding kelas XI Pemasaran
Saprudin Eskom
 
Tugas Praktikum Ms Excel.pdf
Tugas Praktikum Ms Excel.pdfTugas Praktikum Ms Excel.pdf
Tugas Praktikum Ms Excel.pdf
Saprudin Eskom
 
Tugas Praktikum Ms. Word.pdf
Tugas Praktikum Ms. Word.pdfTugas Praktikum Ms. Word.pdf
Tugas Praktikum Ms. Word.pdf
Saprudin Eskom
 
6. percabangan 2 kondisi
6. percabangan 2 kondisi6. percabangan 2 kondisi
6. percabangan 2 kondisi
Saprudin Eskom
 
4. tipe data dan operator java
4. tipe data dan operator java4. tipe data dan operator java
4. tipe data dan operator java
Saprudin Eskom
 
5. algoritma percabangan 1 kondisi
5. algoritma percabangan 1 kondisi5. algoritma percabangan 1 kondisi
5. algoritma percabangan 1 kondisi
Saprudin Eskom
 
1. konsep pbo
1. konsep pbo1. konsep pbo
1. konsep pbo
Saprudin Eskom
 
2. prosedur pbo
2. prosedur pbo2. prosedur pbo
2. prosedur pbo
Saprudin Eskom
 
3. struktur program java
3. struktur program java3. struktur program java
3. struktur program java
Saprudin Eskom
 
17. aplikasi crud java sederhana menggunakan database my sql
17. aplikasi crud java sederhana menggunakan database my sql17. aplikasi crud java sederhana menggunakan database my sql
17. aplikasi crud java sederhana menggunakan database my sql
Saprudin Eskom
 
14. java class library
14. java class library14. java class library
14. java class library
Saprudin Eskom
 
12. tambahan polymorphisme
12. tambahan polymorphisme12. tambahan polymorphisme
12. tambahan polymorphisme
Saprudin Eskom
 
17. aplikasi crud java sederhana menggunakan database my sql
17. aplikasi crud java sederhana menggunakan database my sql17. aplikasi crud java sederhana menggunakan database my sql
17. aplikasi crud java sederhana menggunakan database my sql
Saprudin Eskom
 
16. error handling
16. error handling16. error handling
16. error handling
Saprudin Eskom
 
15. konsep java class library (jcl)
15. konsep java class library (jcl)15. konsep java class library (jcl)
15. konsep java class library (jcl)
Saprudin Eskom
 
12. polymorphisme
12. polymorphisme12. polymorphisme
12. polymorphisme
Saprudin Eskom
 

More from Saprudin Eskom (20)

soal + jawaban PAT DPPLG kelas X PPLG.docx
soal + jawaban PAT DPPLG kelas X PPLG.docxsoal + jawaban PAT DPPLG kelas X PPLG.docx
soal + jawaban PAT DPPLG kelas X PPLG.docx
 
Soal + jawaban PAT PBO Kelas XI RPL 2021.docx
Soal + jawaban PAT PBO Kelas XI RPL 2021.docxSoal + jawaban PAT PBO Kelas XI RPL 2021.docx
Soal + jawaban PAT PBO Kelas XI RPL 2021.docx
 
RPMS PAT PBO XI.docx
RPMS PAT PBO XI.docxRPMS PAT PBO XI.docx
RPMS PAT PBO XI.docx
 
RPMS PAT DPPLG X.docx
RPMS PAT DPPLG X.docxRPMS PAT DPPLG X.docx
RPMS PAT DPPLG X.docx
 
PAS Ganjil Digital Branding kelas XI Pemasaran
PAS Ganjil Digital Branding kelas XI PemasaranPAS Ganjil Digital Branding kelas XI Pemasaran
PAS Ganjil Digital Branding kelas XI Pemasaran
 
Tugas Praktikum Ms Excel.pdf
Tugas Praktikum Ms Excel.pdfTugas Praktikum Ms Excel.pdf
Tugas Praktikum Ms Excel.pdf
 
Tugas Praktikum Ms. Word.pdf
Tugas Praktikum Ms. Word.pdfTugas Praktikum Ms. Word.pdf
Tugas Praktikum Ms. Word.pdf
 
6. percabangan 2 kondisi
6. percabangan 2 kondisi6. percabangan 2 kondisi
6. percabangan 2 kondisi
 
4. tipe data dan operator java
4. tipe data dan operator java4. tipe data dan operator java
4. tipe data dan operator java
 
5. algoritma percabangan 1 kondisi
5. algoritma percabangan 1 kondisi5. algoritma percabangan 1 kondisi
5. algoritma percabangan 1 kondisi
 
1. konsep pbo
1. konsep pbo1. konsep pbo
1. konsep pbo
 
2. prosedur pbo
2. prosedur pbo2. prosedur pbo
2. prosedur pbo
 
3. struktur program java
3. struktur program java3. struktur program java
3. struktur program java
 
17. aplikasi crud java sederhana menggunakan database my sql
17. aplikasi crud java sederhana menggunakan database my sql17. aplikasi crud java sederhana menggunakan database my sql
17. aplikasi crud java sederhana menggunakan database my sql
 
14. java class library
14. java class library14. java class library
14. java class library
 
12. tambahan polymorphisme
12. tambahan polymorphisme12. tambahan polymorphisme
12. tambahan polymorphisme
 
17. aplikasi crud java sederhana menggunakan database my sql
17. aplikasi crud java sederhana menggunakan database my sql17. aplikasi crud java sederhana menggunakan database my sql
17. aplikasi crud java sederhana menggunakan database my sql
 
16. error handling
16. error handling16. error handling
16. error handling
 
15. konsep java class library (jcl)
15. konsep java class library (jcl)15. konsep java class library (jcl)
15. konsep java class library (jcl)
 
12. polymorphisme
12. polymorphisme12. polymorphisme
12. polymorphisme
 

Recently uploaded

Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
Jheel Barad
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
EduSkills OECD
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Atul Kumar Singh
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
Peter Windle
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Po-Chuan Chen
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
CarlosHernanMontoyab2
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
DhatriParmar
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 

Recently uploaded (20)

Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 

13. memahami interface di java (dan contohnya)

  • 1. MEMAHAMI INTERFACE DI JAVA (DAN CONTOHNYA) Oleh: Saprudin Komala, S.Kom.
  • 2. • Ada yang bilang gini: • “Interface adalah blue print dari class. Isi method-nya kosong dan nanti akan diimplementasikan pada class lain.” • Kamu bisa paham maksudnya? • Tidak? • Sama.. hehe 😄 • Ya, seperti itulah penjelasan yang saya terima saat baru awal belajar Java dan OOP dulu. Bukan tambah paham, malah bikin bingung. • Interface memang tak memiliki isi method dan pada tutorial sebelumnya kita juga pakai interface untuk beberapa program java. • Tapi bukan itu sebenarnya kegunaan utama dari Interface. • Lalu apa donk? • Mari kita bahas.
  • 3. APA ITU INTERFACE? • Interface terdiri dari dua kata: • Inter = antar • Face = muka • Jadi interface adalah antarmuka. • Kata ini mungkin sering kamu dengar. Contohnya seperti: GUI (Graphical User Interface) dan CLI (Command Line Interface). • Secara umum, interface berfungsi sebagai penghubung antara sesuatu yang ‘abstrak’ dengan sesuatu yang nyata. • Contoh: • Coba perhatikan handphone-mu, ada berapa tombol di sana? • Kalau HP zaman sekarang, paling cuma ada dua.. yakni tombol power dan tombol volume. • Tombol-tombol inilah yang dimaksud interface. Sementara isi dalam HP-nya, kamu tak peduli atau tak akan tahu seberapa kompleks dan abstrak sistem yang ada di sana.
  • 4.
  • 5. • Saat kamu menekan tombol di HP mu, ada beberapa hal yang mungkin terjadi: • HP menyala; • HP mati; • Suara HP tambah besar; • Suara HP mengecil; • Mengambil gambar dari kamera; dll. • Ini tergantung bagaimana implementasi yang diberikan untuk tombol-tombol tersebut. • Beda merek HP, beda lagi cara mereka mengimplementasikannya. • Misal pada HP tertentu, jika menekan tombol volume 3x.. maka akan mengambil screenshot. Sementara di HP merek lain, kita tidak bisa melakukan ini. • Nah, konsep ini juga ada di dalam OOP. • Bagaimana Contohnya? • Mari kita lihat:
  • 6. CONTOH PROGRAM INTERFACE • Biar nyambung dengan penjelasan saya yang tadi, kita akan pakai contoh Handphone. • Jadi.. ada dua objek yang akan kita hubungkan dengan interface. • Yakni manusia dan handphone. Manusianya kita sebut saja sebagai PhoneUser. Karena dia akan menggunakan objek HP . • Maka dapat kita buat class diagramnya seperti ini:
  • 7.
  • 8. • Sekarang mari kita coba buat dalam bentuk kode. Buatlah proyek baru dengan nama ContohInterface di Netbeans atau text editor apapun.
  • 9.
  • 10.
  • 12.
  • 14. • Sekarang coba jalankan class Main. Caranya, Klik kanan lalu pilih Run File atau bisa juga dengan menekan Shift+F6. • Maka hasilnya: • Kalau kita perhatikan di dalam method main() kita hanya menggunakan objek dari class PhoneUser saja.
  • 15. • Coba lihat kode-kode ini: • Saat kita memanggil method-method ini, maka method yang ada di dalam objek redmiNote8 yang akan dipanggil. • Ini karena mereka terhubung dengan interface. • Lalu pertanyaannya:
  • 16. MENGAPA HARUS PAKAI INTERFACE? • Sebenarnya kita tidak harus selalu menggunakan interface. Tapi dalam kasus tertentu, interface akan sangat berguna. • Misalnya gini: • Kita tahu objek PhoneUser membutuhkan objek dari handphone. Tanpa objek handphone, si objek user nggak akan bisa kita buat. • Ini karena kita menetapkan pada konstruktornya. • Btw, ini juga disebut dengan hubungan dependency.
  • 17. • Nah, di sana kita menggunakan interface Phone untuk membuat objek dependency-nya. • Kalau gak pakai interface memangnya kenapa? • Untuk menjawab pertanyaan ini, mari kita coba ubah Phone menjadi Xiaomi.
  • 18. • Apa yang akan terjadi? • Untuk saat ini tidak akan terjadi apa-apa. Tapi sekarang bagaimana kalau kita punya objek baru dari class yang mengimplementasikan interface Phone juga. • Kita sebut saja objek baru tersebut adalah iPhoneX yang merupakan instance dari class IPhone.
  • 19. • Maka object iPhoneX tidak akan bisa diberikan kepada PhoneUser. Karena di konstruktornya dia cuma bisa pakai Xiaomi saja. Jadi.. Mengapa kita harus pakai interface? Interface akan membuat si objek PhoneUser bisa menggunaakn objek hand-phone apapun dengan syarat objek handphone tersebut harus mengimple-mentasikan method dari interface Phone. Jelas?
  • 20. HAL-HAL YANG TIDAK BOLEH DILAKUKAN DI INTERFACE • Ada beberapa hal yang tidak boleh kamu lakukan saat membuat interface: • Jangan buat variabel di dalam interface, tapi membuat konstanta boleh; • Jangan mengisi method-nya, cukup tuliskan nama method, tipe data, dan parameter saja. Tapi untuk default metod boleh punya isi. • Contoh: • Jangan berikan modifier privete atauapun protected pada method dan konstanta yang ada di dalam interface. • Interface tidak bisa dibuat objek instance-nya dengan kata kunci new. • Kalau dilanggar gimana? • Ya programnya akan error!
  • 21. APA SELANJUTNYA? • Jadi kesimpulannya: • Interface merupakan penghubung antar objek. Interface bersifat abstrak, sehingga objek yang menggunakannya tidak akan perduli bagaimana ia diimplementasikan. • Karena bersifat abstrak, interface tidak bisa dibuat objek instance dengan kata kunci new. • Interface sebenarnya mengamalkan prinsip Abstraksi dan Enkapsulasi (pembungkusan). • Nah, itu saja yang bisa saya bagikan tentang interface. • Selanjutnya, silahkan kalian praktekkan program interface di atas.
  • 22. • Sumber referensi: https://www.petanikode.com/java-oop-interface/