Jumat, 02 November 2012

METODE ABSTRACT

ABSTRACT CLASS, INTERFACE, INNER CLASS

  1. Abstract class adalah suatu kelas yang dinyatakan abstract, umumnya memiliki suatu atau lebih abstract method.Abstract method adalah suatu method yang memiliki implementasi dan menggunakan modifier abstract. Contoh :
    abstract class A {
      abstract void callme();

      void callmetoo() {
        System.out.println("This is a concrete method.");
      }
    }

    class extends A {
      void callme() {
        System.out.println("B's implementation of callme.");
      }
    }

    class AbstractDemo {
      public static void main(String args[]) {
        B b = new B();

        b.callme();
        b.callmetoo();
      }
    }
  2. Interface adalah suatu kelas yang berisi method-method tanpa implementasi, namun tanpa modifier abstract, apabila suatu interface memiliki atribut, maka atributnya akan berlaku sebagai konstanta. Contoh :
    interface ThisInterface {
      public void thisMethod();
    }

    interface ThatInterface {
      public void thatMethod();
    }

    class MyClass implements ThisInterface, ThatInterface {
      // Class definition including methods from both interfaces...
      public void thisMethod() {
        System.out.println("this");
      }

      public void thatMethod() {
        System.out.println("that");
      }
    }

    public class MainClass {
      public static void main(String[] a) {
        MyClass cls = new MyClass();
        cls.thisMethod();
        cls.thatMethod();
      }
    }
    outputnya adalah : 
    this
    that
  3. Inner class adalah kelas yang disisipkan di dalam kelas yang lain. Fungsi kelas ini digunakan untuk mendukung suatu proses yang akan dijalankan oleh kelas utamanya.