Protected V V V X
Public V V V V
Pendefinisian Kelas - JAVA
• Secara umum seluruh attribute pada kelas akan
memiliki access modifier private (menjamin
information hiding)
• Konsekuensi
▫ Disediakan method untuk mengakses
Mutator/Setter
Accessor/Getter
Declaring Java Classes
Class_name
Basic syntax of Java class :
<class_declaration> ::= - attribute_declaration
<modifier> class <class_name> { + constructor_declaration
<attribute_declaration>* + method_declaration
<constructor_declaration>*
<method_declaration>*
}
Example :
1. public class Student {
2. private String name; Student
3. public void setName(String s) {
- name : string
4. name = s;
5. } + setName(s:string)
6. }
Declaring Attributes
Basic syntax of Java class :
<attribute_declaration> ::=
<modifier> <type>* <name> [=<default_value>];
<type> ::= byte | short | int | long | char | float |
double | boolean | <class_name>
Example :
1. public class Foo {
2. private int x;
3. public float y = 10000.0;
4. public String name = "Alfred";
5. }
Declaring Methods
Basic syntax of Java class :
<method_declaration> ::=
<modifier> <return_type> <name> (<parameter>*) {
<statement>
}
Example :
1. public class Dog {
2. private int weight;
3. public int getWeight() {
4. return weight;
5. }
6. public void setWeight(int newWeight) {
7. weight = newWeight;
8. }
9. }
Information Hiding
• The Problem :
MyDate d = new MyDate();
d.day = 32;
MyDate
//invalid day +day : int
d.month = 2; d.day = 30; +month : int
//plausible but wrong +year : int
d.day = d.day + 1;
//no check for wrap around
14
Information Hiding
The Solution
1. public class MyDate { MyDate
2. private int day;
3. private int month; -day : int
4. private int year;
-month : int
5. public int getDay() { -year : int
6. return day;
7. } +getDay() : int
8. public int getMonth() {
9. return month; +getMonth() : int
10.
11.
}
public int getYear() {
+getYear() : int
12. return year; +setDay(in d : int)
13. }
14. public void setDay(int d) { +setMonth(in m : int)
15. day = d; +setYear(in y : int)
16. }
17. public void setMonth(int m) {
18. month = m;
19. }
20. public void setDay(int y) {
21. year = y;
22. }
23.}
Declaring Constructors
Basic syntax of a constructor :
<constructor_declaration> ::=
<modifier> <class_name> (<parameter>*) {
<statement>
}
Example :
1. public class Thing {
2. private int x;
3. public Thing() {
4. x = 47;
5. }
6. public Thing(int new_x) { // overloaded constructor
7. x = new_x;
8. }
9. }
Declaring Constructors
1. public class Thing {
2. private int x;
3. public Thing() {
4. x = 47;
5. }
6. public Thing(int new_x) { // overloaded constructor
7. x = new_x;
8. }
9. public int getX() { // a 'getter'
10. return x;
11. }
12. public void setX(int new_x) { // a 'setter'
13. x = new_x;
14. }
15. }
Declaring Constructors
Example usage :
1. public class TestThing {
2. public static void main(String[] args) {
3. Thing thing1 = new Thing();
4. Thing thing2 = new Thing(42);
Output :
thing1.x is 47
thing2.x is 42
Encapsulasi
• Sample Code
▫ Tambahkan kode untuk menunjukkan perbedaan
makna pemakaian access-specifier
Studi Kasus 1
• Pengolahan bangun geometri
▫ Segitiga
▫ Lingkaran
▫ Persegi Panjang
• Note: Tidak ada hubungan khusus antar kelas,
setiap kelas dibuat pada file .java
• Pembuatan kelas driver untuk eksekusi program
Diagram kelas
Driver