Rahmadnet

Interface

Jan 16th, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.13 KB | None | 0 0
  1. 1. what is Interface
  2.     Interface adalah kumpulan method secara terstruktur yang berfungsi untuk tempat menyimpan
  3.     method kosong dan atribut yang bersifat constant atau final (bisa disebut juga template method),
  4.     serta tidak mempunyai implementasi atau tidak memiliki body.
  5. 2. what are their benefit
  6.     Interface bermanfaat untuk membuat method secara terstruktur sehingga code menjadi rapi
  7.     sehingga mudah di pahami.
  8. 3. what are their drawback
  9.     Tidak bisa membuat instance atau objek baru dari sebuah interface.
  10. 4. when to use it at best
  11.     Interface digunakan pada saat kita ingin mengimplementasikan subclass dari interface
  12.     kerena subclass ingin menngunakan method dari interface.
  13. 5. when to NOT use it
  14.     Interface tidak digunakan pada saat kita ingin mengimplementasikan interface
  15.     dengan abstrak karena interface tidak bisa mengimplementasikan class abstract.
  16.  
  17.  
  18. public class aplikasi {
  19.  
  20.     public static void main(String[] args) {
  21.         Super obj = new Base();
  22.         obj.patern1();
  23.         System.out.println("");
  24.         obj.patern2();
  25.         System.out.println("");
  26.         Super.patern3();
  27.     }
  28. }
  29.  
  30. interface Super {
  31.     /**
  32.      * An abstract method. By default it is public and abstract.
  33.      */
  34.     void patern1();
  35.  
  36. public default void patern2() {
  37.         int x,y;
  38.         for(y = 1; y <= 9; y++) {
  39.             for(x =1; x <= y; x++) {
  40.                 System.out.print("*");
  41.             }
  42.             System.out.println();
  43.         }
  44.     }
  45.  
  46.     public static void patern3() {
  47.        int x,y;
  48.        for(y = 1; y <= 9; y++) {
  49.            for(x = 1; x <= y; x++) {
  50.                System.out.print(y);
  51.            }
  52.            System.out.println();
  53.        }
  54.     }
  55. }
  56.  
  57. class Base implements Super {
  58.     /**
  59.      * Override the abstract method.
  60.      */
  61.     @Override
  62.     public void patern1() {
  63.        
  64.         int x , y ;
  65.         for(y = 1; y <= 9; y++) {
  66.             for(x = 1; x <= 9; x++) {
  67.                 System.out.print("*");
  68.             }
  69.             System.out.println();
  70.         }
  71.         System.out.println("");
  72.        
  73.         for(y = 1; y <= 9; y++) {
  74.             for(x = 1; x <= 9; x++) {
  75.                 System.out.print(y);
  76.             }
  77.             System.out.println();
  78.         }
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment