Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1. what is Interface
- Interface adalah kumpulan method secara terstruktur yang berfungsi untuk tempat menyimpan
- method kosong dan atribut yang bersifat constant atau final (bisa disebut juga template method),
- serta tidak mempunyai implementasi atau tidak memiliki body.
- 2. what are their benefit
- Interface bermanfaat untuk membuat method secara terstruktur sehingga code menjadi rapi
- sehingga mudah di pahami.
- 3. what are their drawback
- Tidak bisa membuat instance atau objek baru dari sebuah interface.
- 4. when to use it at best
- Interface digunakan pada saat kita ingin mengimplementasikan subclass dari interface
- kerena subclass ingin menngunakan method dari interface.
- 5. when to NOT use it
- Interface tidak digunakan pada saat kita ingin mengimplementasikan interface
- dengan abstrak karena interface tidak bisa mengimplementasikan class abstract.
- public class aplikasi {
- public static void main(String[] args) {
- Super obj = new Base();
- obj.patern1();
- System.out.println("");
- obj.patern2();
- System.out.println("");
- Super.patern3();
- }
- }
- interface Super {
- /**
- * An abstract method. By default it is public and abstract.
- */
- void patern1();
- public default void patern2() {
- int x,y;
- for(y = 1; y <= 9; y++) {
- for(x =1; x <= y; x++) {
- System.out.print("*");
- }
- System.out.println();
- }
- }
- public static void patern3() {
- int x,y;
- for(y = 1; y <= 9; y++) {
- for(x = 1; x <= y; x++) {
- System.out.print(y);
- }
- System.out.println();
- }
- }
- }
- class Base implements Super {
- /**
- * Override the abstract method.
- */
- @Override
- public void patern1() {
- int x , y ;
- for(y = 1; y <= 9; y++) {
- for(x = 1; x <= 9; x++) {
- System.out.print("*");
- }
- System.out.println();
- }
- System.out.println("");
- for(y = 1; y <= 9; y++) {
- for(x = 1; x <= 9; x++) {
- System.out.print(y);
- }
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment