You are on page 1of 10

CHAPTER 3.

Object Oriented Programming dengan Java


Disampaikan pada Mata Kuliah OOP with Java

Eddy Muntina Dharma,ST,MT
aguseddy@stttelkom.ac.id
aguseddy@yahoo.com
EMD-OOP 2
Classes as Blueprints for Objects
Dalam dunia manufaktur, blueprint bisa berarti
deskripsi dari device yang akan dibuat atau
dibangun.
Dalam dunia software, class adalah deskripsi dari
object, yaitu :
Dalam class menjelaskan data/atribut/property dari object
Dalam class menjelaskan perilaku/operasi/fungsi/method
yang bisa dilakukan oleh object.
Pada Java, class mendukung 3 features dari OOP :
Encapsulation
Inheritance
Polymorphism
EMD-OOP 3
Declaring Java Classes
Basic syntax of Java class :
<class_declaration> ::=
<modifier> class <class_name> {
<attribute_declaration>*
<constructor_declaration>*
<method_declaration>*
}

Example :
1. public class Student {
2. private String name;
3. public void setName(String s) {
4. name = s;
5. }
6. }
Student
- name : string
+ setName(s:string)


Class_name
- attribute_declaration
+ constructor_declaration
+ method_declaration


EMD-OOP 4
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. }
EMD-OOP 5
Declaring Methods
Basic syntax of Java class :
<method_declaration> ::=
<modifier> <return_type> <name> (<parameter>*) {
<statement>
}

<parameter> ::= <parameter_type> <parameter_name>

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. }
EMD-OOP 6
Information Hiding
The Problem :
MyDate d = new MyDate();
d.day = 32;
//invalid day

d.month = 2; d.day = 30;
//plausible but wrong

d.day = d.day + 1;
//no check for wrap around
+day : int
+month : int
+year : int
MyDate
7
Information Hiding
The Solution
1. public class MyDate {
2. private int day;
3. private int month;
4. private int year;

5. public int getDay() {
6. return day;
7. }
8. public int getMonth() {
9. return month;
10. }
11. public int getYear() {
12. return year;
13. }
14. public void setDay(int d) {
15. day = d;
16. }
17. public void setMonth(int m) {
18. month = m;
19. }
20. public void setDay(int y) {
21. year = y;
22. }
23. }
+getDay() : int
+getMonth() : int
+getYear() : int
+setDay(in d : int)
+setMonth(in m : int)
+setYear(in y : int)
-day : int
-month : int
-year : int
MyDate
EMD-OOP 8
Declaring Constructors
Basic syntax of a constructor :
<constructor_declaration> ::=
<modifier> <class_name> (<parameter>*) {
<statement>
}

<parameter> ::= <parameter_type> <parameter_name>

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. }
EMD-OOP 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. }
EMD-OOP 10
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);

5. System.out.println("thing1.x is " + thing1.getX());
6. System.out.println("thing2.x is " + thing2.getX());
7. }
8. }

Output :
thing1.x is 47
thing2.x is 42

You might also like