Anda di halaman 1dari 11

Abstraction in Java or Object oriented programming is a way to segregate implementation from interface and one of the five fundamentals

along with Encapsulation, Inheritance, Polymorphism, Class and Object. Abstraction in Java is achieved by using interface and abstract class in Java. An interface or abstract class is something which is not concrete , something which is incomplete. In order to use interface or abstract class we need to extend and implement abstract method with concrete behavior. One example of Abstraction is creating interface to denote common behavior without specifying any details about how that behavior works e.g. You create an interface called Server which has start() and stop() method. This is called abstraction of Server because every server should have way to start and stop and details may differ. As I said earlier Abstraction in Java is implemented using abstract class and interface as discussed in next section. In fact What is abstraction in Java, Difference between Abstraction and Encapsulation is also a very popular core Java interview because strong OOPS skill is one of the primary requirement for Java developers. Abstraksi dalam pemrograman Java atau Object berorientasi adalah cara untuk memisahkan implementasi dari interface dan salah satu dari lima dasar bersama dengan Encapsulation, Inheritance, Kelas Polimorfisme, dan Object. Abstraksi di Jawa dicapai dengan menggunakan antarmuka dan kelas abstrak di Jawa. Sebuah antarmuka atau kelas abstrak adalah sesuatu yang tidak konkret, sesuatu yang tidak lengkap. Untuk menggunakan antarmuka atau kelas abstrak kita perlu memperluas dan menerapkan metode abstrak dengan perilaku beton. Salah satu contoh Abstraksi adalah menciptakan antarmuka untuk menunjukkan perilaku umum tanpa menentukan rincian apapun tentang bagaimana perilaku yang bekerja misalnya Anda membuat sebuah antarmuka yang disebut Server yang memiliki start () dan stop () metode. Ini disebut abstraksi dari Server karena server harus dimiliki setiap cara untuk memulai dan menghentikan dan rincian mungkin berbeda. Seperti yang saya katakan sebelumnya Abstraksi di Jawa diimplementasikan dengan menggunakan kelas abstrak dan interface seperti yang dibahas dalam bagian berikutnya. Bahkan Apa abstraksi di Jawa, Perbedaan antara Abstraksi dan Enkapsulasi juga inti sangat populer Jawa wawancara karena kuat OOPS keterampilan merupakan salah satu persyaratan utama untuk pengembang Java.

Read more: http://javarevisited.blogspot.com/2010/10/abstraction-injava.html#ixzz2NMf9pQ6y JButton ok = new JButton("OK"); ok.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ //code to handle event } });

An abstract method in Java doesn't have body , its just a declaration. In order to use abstract method you need to override that method in SubClass. so when do you use abstraction ? ( most important in my view ) when Yo know something needs to be there but not sure how exactly it should look like. e.g. when I am creating a class called Vehicle, I know there should be methods like start() and Stop() but don't know start and stop mechanism of every vehicle since they could have different start and stop mechanism e..g some can be started by kick or some can be by pressing buttons . the same concept apply to interface in Java also , which we will discuss in some other post. so implementation of those start() and stop() methods should be left to there concrete implementation e.g. Scooter , MotorBike , Car etc. Sebuah metode abstrak di Jawa tidak memiliki tubuh, hanya deklarasi a. Untuk menggunakan metode abstrak anda ingin merubah metode yang di subclass. jadi ketika Anda menggunakan abstraksi? (Paling penting dalam pandangan saya) ketika Yo tahu sesuatu harus ada tapi tidak yakin bagaimana tepatnya itu harus terlihat seperti. misalnya ketika saya membuat kelas yang disebut Kendaraan, saya tahu harus ada metode seperti start () dan Stop () tetapi tidak tahu memulai dan menghentikan mekanisme setiap kendaraan karena mereka bisa memiliki start yang berbeda dan menghentikan mekanisme e .. g sebagian dapat akan dimulai oleh tendangan atau dapat juga dengan menekan tombol. konsep yang sama berlaku untuk antarmuka di Jawa juga, yang akan kita bahas dalam beberapa pos lainnya. sehingga pelaksanaan mereka start () dan stop () metode harus diserahkan ke sana misalnya implementasi konkret Scooter, sepeda motor, mobil dll

In Java Interface is an another way of providing abstraction, Interfaces are by default abstract and only contains public static, final constant or abstract methods. Its very common interview question is that where should we use abstract class and where should we use Java Interfaces in my view this is important to understand to design

better Java application, you can go for java interface if you only know the name of methods your class should have e.g. for Server it should have start() and stop() method but we don't know how exactly these start and stop method will work. if you know some of the behavior while designing class and that would remain common across all subclasses add that into abstract class. Interface like Runnable are good example of abstraction in Java which is used to abstract task executed by multiple thread. in Summary 1) Use abstraction if you know something needs to be in class but implementation of that varies. 2) In Java you can not create instance of abstract class , its compiler error. 3) abstract is a keyword in java. 4) a class automatically becomes abstract class when any of its method declared as abstract. 5) abstract method doesn't have method body. 6) variable can not be made abstract , its only behavior or methods which would be abstract. 7) If a class extends an abstract class or interface it has to provide implementation to all its abstract method to be a concrete class. alternatively this class can also be abstract. Di Jawa Antarmuka adalah cara lain untuk memberikan abstraksi, Interface adalah dengan abstrak default dan hanya berisi public static, metode konstan atau abstrak akhir. Pertanyaan wawancara yang sangat umum adalah bahwa di mana kita harus menggunakan kelas abstrak dan di mana kita harus menggunakan Interfaces Java dalam pandangan saya ini adalah penting untuk memahami lebih baik untuk merancang aplikasi Java, Anda dapat pergi untuk antarmuka java jika Anda hanya tahu nama metode kelas Anda seharusnya misalnya untuk Server harus memiliki start () dan stop () metode tetapi kita tidak tahu bagaimana sebenarnya metode start dan stop akan bekerja. jika Anda tahu beberapa perilaku saat merancang kelas dan itu akan tetap umum di semua subclass menambahkan bahwa dalam kelas abstrak. Antarmuka seperti Runnable adalah contoh yang baik dari abstraksi di Jawa yang digunakan untuk tugas yang abstrak dieksekusi oleh beberapa thread.

dalam Ringkasan 1) Gunakan abstraksi jika Anda tahu sesuatu harus di kelas, tetapi implementasi yang bervariasi. 2) Di Jawa Anda tidak dapat membuat instance dari kelas abstrak, kesalahan penyusunnya.

3) abstrak adalah kata kunci dalam java. 4) kelas otomatis menjadi kelas abstrak ketika salah satu metode yang dinyatakan sebagai abstrak. 5) metode abstrak tidak memiliki metode tubuh. 6) variabel tidak dapat dibuat abstrak, perilaku satunya atau metode yang akan menjadi abstrak. 7) Jika kelas meluas kelas abstrak atau antarmuka itu harus menyediakan implementasi untuk semua metode abstrak untuk menjadi kelas beton. alternatif kelas ini juga dapat menjadi abstrak. Read more: http://javarevisited.blogspot.com/2010/10/abstraction-injava.html#ixzz2NMfUvPjv

Abstraction refers to the ability to make a class abstract in OOP. An abstract class is one that cannot be instantiated. All other functionality of the class still exists, and its fields, methods, and constructors are all accessed in the same manner. You just cannot create an instance of the abstract class. If a class is abstract and cannot be instantiated, the class does not have much use unless it is subclassed. This is typically how abstract classes come about during the design phase. A parent class contains the common functionality of a collection of child classes, but the parent class itself is too abstract to be used on its own.

Abstraksi mengacu pada kemampuan untuk membuat abstrak kelas dalam OOP. Kelas abstrak adalah salah satu yang tidak dapat instantiated. Semua fungsi lain dari kelas masih ada, dan bidangnya, metode, dan konstruktor semua diakses dengan cara yang sama. Anda hanya tidak dapat membuat sebuah instance dari kelas abstrak. Jika kelas yang abstrak dan tidak dapat instantiated, kelas tidak memiliki banyak digunakan kecuali subclassed. Ini biasanya bagaimana kelas abstrak terjadi selama fase desain. Sebuah kelas induk berisi fungsi umum dari kumpulan anak kelas, tetapi kelas induk itu sendiri terlalu abstrak untuk digunakan sendiri. Abstract Class:
Use the abstract keyword to declare a class abstract. The keyword appears in the class declaration somewhere before the class keyword. Gunakan kata kunci abstrak untuk menyatakan suatu kelas abstrak. Kata kunci muncul dalam deklarasi kelas di suatu tempat sebelum kata kunci kelas /* File name : Employee.java */ public abstract class Employee { private String name; private String address; private int number; public Employee(String name, String address, int number) {

System.out.println("Constructing an Employee"); this.name = name; this.address = address; this.number = number; } public double computePay() { System.out.println("Inside Employee computePay"); return 0.0; } public void mailCheck() { System.out.println("Mailing a check to " + this.name + " " + this.address); } public String toString() { return name + " " + address + " " + number; } public String getName() { return name; } public String getAddress() { return address; } public void setAddress(String newAddress) { address = newAddress; } public int getNumber() { return number; } } Notice that nothing is different in this Employee class. The class is now abstract, but it still has three fields, seven methods, and one constructor. Now if you would try as follows: Perhatikan bahwa tidak ada yang berbeda dalam kelas Karyawan. Kelas ini sekarang abstrak, tetapi masih memiliki tiga bidang, tujuh metode, dan satu konstruktor. Sekarang jika Anda akan mencoba sebagai berikut: /* File name : AbstractDemo.java */ public class AbstractDemo { public static void main(String [] args) { /* Following is not allowed and would raise error */ Employee e = new Employee("George W.", "Houston, TX", 43); System.out.println("\n Call mailCheck using Employee reference--"); e.mailCheck(); }

} When you would compile above class then you would get following error: Ketika Anda akan mengkompilasi atas kelas maka Anda akan mendapatkan error berikut:

Employee.java:46: Employee is abstract; cannot be instantiated Employee e = new Employee("George W.", "Houston, TX", 43); ^ 1 error

Extending Abstract Class:


We can extend Employee class in normal way as follows: Memperluas Kelas Abstrak: Kita dapat memperpanjang kelas Karyawan dengan cara normal sebagai berikut: /* File name : Salary.java */ public class Salary extends Employee { private double salary; //Annual salary public Salary(String name, String address, int number, double salary) { super(name, address, number); setSalary(salary); } public void mailCheck() { System.out.println("Within mailCheck of Salary class "); System.out.println("Mailing check to " + getName() + " with salary " + salary); } public double getSalary() { return salary; } public void setSalary(double newSalary) { if(newSalary >= 0.0) { salary = newSalary; } } public double computePay() { System.out.println("Computing salary pay for " + getName()); return salary/52; } } Here we cannot instantiate a new Employee, but if we instantiate a new Salary object, the Salary object will inherit the three fields and seven methods from Employee.

Di sini kita tidak bisa instantiate Karyawan baru, tetapi jika kita instantiate objek Gaji baru, objek Gaji akan mewarisi tiga bidang dan tujuh metode dari karyawan. /* File name : AbstractDemo.java */ public class AbstractDemo { public static void main(String [] args) { Salary s = new Salary("Mohd Mohtashim", "Ambehta, 3, 3600.00); Salary e = new Salary("John Adams", "Boston, MA", 2, 2400.00);

UP",

System.out.println("Call mailCheck using Salary reference --"); s.mailCheck(); System.out.println("\n Call mailCheck using Employee reference--"); e.mailCheck(); } } This would produce following result: Constructing an Employee Constructing an Employee Call mailCheck using Salary reference -Within mailCheck of Salary class Mailing check to Mohd Mohtashim with salary 3600.0 Call mailCheck using Employee reference-Within mailCheck of Salary class Mailing check to John Adams with salary 2400.

Abstract Methods:
If you want a class to contain a particular method but you want the actual implementation of that method to be determined by child classes, you can declare the method in the parent class as abstract. The abstract keyword is also used to declare a method as abstract.An abstract methods consist of a method signature, but no method body. Abstract method would have no definition, and its signature is followed by a semicolon, not curly braces as follows: Abstrak Metode: Jika Anda ingin kelas mengandung metode tertentu tetapi Anda ingin pelaksanaan sebenarnya metode yang akan ditentukan oleh anak kelas, Anda dapat mendeklarasikan metode di kelas induk sebagai abstrak. Kata kunci abstrak juga digunakan untuk menyatakan metode sebagai abstract.An abstrak metode terdiri dari metode tanda tangan, tetapi tidak ada tubuh metode. Metode abstrak akan memiliki definisi tidak ada, dan tanda tangan yang diikuti oleh titik koma, bukan kurung kurawal sebagai berikut: public abstract class Employee {

private String name; private String address; private int number; public abstract double computePay(); //Remainder of class definition } Declaring a method as abstract has two results:

as well.

The class must also be declared abstract. If a class contains an abstract method, the class must be abstract

Any child class must either override the abstract method or declare itself abstract. A child class that inherits an abstract method must override it. If they do not, they must be abstract,and any of their children must override it. Eventually, a descendant class has to implement the abstract method; otherwise, you would have a hierarchy of abstract classes that cannot be instantiated. If Salary is extending Employee class then it is required to implement computePay() method as follows: Mendeklarasikan metode sebagai abstrak memiliki dua hasil: class juga harus dideklarasikan abstrak. Jika kelas berisi metode abstrak, kelas harus abstrak juga. Setiap kelas anak baik harus mengganti metode abstrak atau menyatakan dirinya abstrak. Seorang anak kelas yang mewarisi metode abstrak harus menimpa itu. Jika tidak, mereka harus abstrak, dan salah satu dari anak-anak mereka harus menimpa itu. Akhirnya, kelas keturunan harus menerapkan metode abstrak, jika tidak, Anda akan memiliki hirarki kelas abstrak yang tidak dapat instantiated. Jika Gaji adalah memperluas kelas Karyawan maka diperlukan untuk melaksanakan computePay () metode sebagai berikut: /* File name : Salary.java */ public class Salary extends Employee { private double salary; // Annual salary public double computePay() { System.out.println("Computing salary pay for " + getName()); return salary/52; } //Remainder of class definition }

Today i heard from my friend, that encapsulation is not only achieving information hiding but also abstraction. How does it achieve? Hari ini saya mendengar dari teman saya, bahwa enkapsulasi tidak hanya mencapai menyembunyikan informasi tetapi juga abstraksi. Bagaimana cara mencapai? public class employee { private String name; private int id; public void setName(String name){ this.name = name; } public String getName(){ return name; } } The above example achieves encapsulation where i am allowing the class to access my public method rather than private members, but where does the abstraction come into picture here? Can anyone explain me on abstraction in a bit clear manner.

Contoh di atas mencapai enkapsulasi mana saya memungkinkan kelas untuk mengakses metode umum saya daripada anggota pribadi, tapi mana abstraksi datang ke dalam gambar di sini? Ada yang bisa menjelaskan saya pada abstraksi secara jelas sedikit.

Anda mungkin juga menyukai