Pemrograman Berorientasi Objek : Kelas, Objek, dan Methods

Pemrograman Berorientasi Objek : Kelas, Objek, dan Methods



Prinsip pemrograman berorientasi objek adalah membagi modul program yang besar menjadi kumpulan objek-objek yang saling berinteraksi, dengan prinsip seperti ini maka pemrograman berorientasi objek lebih menekankan pada proses yang terjadi antar objek. Sehingga dikemudian akan terdapat kemungkinan sebuah program merupakan kumpulan objek-objek yang saling bertukar informasi dan data dan memiliki fungsi dan tanggung jawab masing-masing.


Kelas dan objek merupakan satuan yang berbeda, Kelas Merupakan kumpulan atas definisi data dan fungsi-fungsi dalam suatu unit untuk suatu tujuan tertentu dimana didalamnya terdapat kumpulan atribut dan method,


Contoh kelas : binatang, kendaraan, benda dsb.


Sedangkan objek merupakan bentuk representasi dari sebuah kelas, membungkus data dan fungsi bersama menjadi suatu unit atau entitas dalam sebuah program komputer. pada dasarnya ada 2 karakteristik yang utama pada sebuah objek ;


  • Objek memiliki attribut sebagai status yang disebut Stat
  • Objek memiliki tingkahlaku yang kemudian disebut Method

Contoh sederhananya :




Objek motor memiliki attribute : Roda, Warna, Merk,. Kemudian objek motor tersebut memiliki tingkahlaku / method : pindah gerigi, kecepatan menaik, kecepatan menurun.
contoh objek :


  • Anjing, kucing, kuda : dari kelas binatang
  • Sepeda motor, mobil, pesawat, kapal : dari kelas kendaraan.
  • Batu, air, api, udara : dari kelas benda. dsb


Kelas


Dalam bahasa pemrograman java sebuah file berekstensi *.java harus memiliki sebuah class public dan dapat memiliki beberapa class non public. Contoh deklarasi sebuah kelas binatang




Dari deklarasi kelas diatas terdapat beberapa attribute : jmlKaki, warna, suara. Method pada kelas binatang diatas adalah bersuara.


Objek


Objek merupakan bentuk representasi dari sebuah kelas, membungkus data dan fungsi bersama menjadi suatu unit atau entitas dalam sebuah program komputer, sebuah kelas dapat diinstansiasikan menjadi banyak objek, sebagai contoh sebuah kelas binatang dapat diinstansiasikan menjadi objek : kuda, ayam, kambing, harimau dan sebagainya. 



Dari contoh program diatas dapat diketahui terdapat 3 objek : ayam, kuda, harimau. Dimana masing-masing objek merupakan hasil instansiasi dari sebuah kelas binatang.


Method


Method adalah kumpulan program yang mempunyai nama. Method merupakan sarana bagi programmer untuk memecah program menjadi bagian-bagian yang kecil agar jadi lebih kompleks sehingga dapat di gunakan berulang-ulang.

Method merupakan suatu operasi berupa fungsi-fungsi yang dapat dikerjakan oleh suatu object. Method didefinisikan pada class akan tetapi dipanggil melalui object. Contoh, pada object pear : terdapat method ambilRasa , kupasKulit dan lain lain


Questions




Questions 1


Consider the following class:



  • What are the class variables ?
  • What are the instance variables ? 
  • What is the output from the following code :


Questions 2


What's wrong with the following program ?



The following code creates one array and one string object. How many references to those objects exist after the code executes? Is either object eligible for garbage collection ?


How does a program destroy an object that it creates ?


Answers to Question 1

Consider the following class:


  • What are the class variables ?
Answer: x

  • What are the instance variables ? 
Answer: y

  • What is the output from the following code :

a.y = 5 
b.y = 6 
a.x = 2 
b.x = 2 
IdentifyMyParts.x = 2

Because x is defined as a public static int in the class IdentifyMyParts, every reference to x will have the value that was last assigned because x is a static variable (and therefore a class variable) shared across all instances of the class. That is, there is only one x: when the value of x changes in any instance it affects the value of x for all instances of IdentifyMyParts.


Answers to Question 2

What's wrong with the following program ?


Answer: The code never creates a Rectangle object. With this simple program, the compiler generates an error. However, in a more realistic situation, myRect might be initialized to null in one place, say in a constructor, and used later. In that case, the program will compile just fine, but will generate a NullPointerException at runtime.

The following code creates one array and one string object. How many references to those objects exist after the code executes? Is either object eligible for garbage collection ?


Answer: There is one reference to the students array and that array has one reference to the string Peter Smith. Neither object is eligible for garbage collection. The array students is not eligible for garbage collection because it has one reference to the object studentName even though that object has been assigned the value null. The object studentName is not eligible either because students[0] still refers to it.

How does a program destroy an object that it creates ?

Answer: A program does not explicitly destroy objects. A program can set all references to an object to null so that it becomes eligible for garbage collection. But the program does not actually destroy objects.

Sumber




1 Komentar

Posting Komentar

Lebih baru Lebih lama