Index of /Kuliah2012-2013/PJJ_BANJAR/PBO Day 7

Pemrograman Berorientasi
Obyek
Day 7 :
D
Enkapsulasi & Abstraksi Data - Modifiers

Topik :
Enkapsulasi
p
dan abstraki data
y Modifier
y Package declarations
y Import statements
y Access
A
controll
y Pengenalan UML Æ class
y

UML
What is UML?

y UML = Unified Modeling Language
y UML is a graphical language for modeling
software systems.
y

Information Hiding
Problem :
MyDate
Day : Integer
Month : Integer
Year : Integer

Information Hiding

MyDate
Day : Integer
Month : Integer
Year : Integer
GetDay()
GetMonth()

GetYear()
SetDay()
SetMonth()
SetYear()
ValidDay()
y()

With days in
month

Encapsulation
Menyembunyikan detail implementasi dari kelas
y Untuk mengakses data dari kelas harus melewati
interface (method)
y Kode program lebih mudah dikelola
y

MyDate
Day
y : Integer

g
Month : Integer
Year : Integer
GetDay()
GetMonth()
GetYear()
SetDay()
SetMonth()
SetYear()
ValidDay()

Software Package
Package dapat membantu dalam mengelola sistem
software
f
yang besar
b
y Package dapat berisi kelas – kelas dan subpackage
y


Contoh :
Perusahaan pengantaran (shipping)
y
y
y
y
y

y
y

S/W harus bisa mendukung sebuah perusahaan pengantaran
Perusahaan ini mengelola sejumlah kendaraan yang mengirim
paket
Jumlah berat paket merupakan faktor terpenting dalam
ppemilihan kendaraan
Perusahaan ini memiliki 2 tipe kendaraan yaitu truk dan
angkutan laut
Paket yyangg dikirim diukur dalam satuan kilogram,
g

algoritma
g
untuk menghitung kekuatan mesin kendaraan yang diperlukan
untuk mengangkut paket – paket digunakan satuan Newton
Terdapat GUI untuk memonitor penambahan paket pada
k d
kendaraan
Terdapat beberapa laporan mengenai pengiriman paket

N
Note
: 1 N = 9.8
9 8 kg
k

Shipping Package Direktori
y

Package disimpan pada pohom direktori
yang berisi nama package


Pembuatan pada project
JavaProject/
Classes/
History/
Src/
Domain/
GUI /
Report/

Compiling using –d
Javac –d ../ JavaProject/ Src/ Domain/ * .java

Modifiers
y
y
y

Give the compiler information about the nature of
code data,

code,
data or classes
classes.
Called access modifiers, dictate which classes are allowed
to use a feature.
Can be used in combination to describe the attributes
of a feature.
Note: A feature is a class, a method, or a variable.

Modifier Overview
y

Access Modifiers
◦ public
◦ protected
◦ private

y

Other Modifiers









final
abstract
static
native
transient
synchronized
volatile

The Access Modifiers
Fungsi:
g melakukan kontrol terhadapp class
feature yang menggunakan modifier ini.

y Suatu feature
eatu e minimal
a harus
a us memiliki
e
satu
modifier.
y Yang termasuk class feature:
y

◦ The class itself
◦ Its member variables
◦ Its methods and constructors

The Access Modifiers
Suatu class Yyy
yy mempunyai
p y akses ke class
Xxx.
y Arti

t Æ cclass
ass Yyy
yy tersebut
te sebut mampu:
a pu:
y

◦ Membuat instance dari class Xxx.
◦ Melakukan extend terhadap class Xxx.
◦ Mengakses methods dan variabel yg ada dalam
class Xxx.

Declaration
y

Legal Declaration







y

class Parser { ... }
public class EightDimensionalComplex { ... }
private int i;
Graphics offScreenGC;
protected double getChiSquared() { ... }

Illegal Declaration
◦ public protected int x; // At most 1 access modifier
◦ default Button getBtn() {...} // “default” isn’t a keyword

private
y

Digunakan oleh:
◦ variabel
b l
◦ method

y

y
y

Variabel dan method yang dideklarasikan
private hanya bisa diakses oleh instance dari
class yg mendeklarasikan variabel dan
method
th d ttersebut.
b t
Private variabel dan method dari class Xxx
hanya bisa diakses melalui (within) class Xxx.
Instance dari subclass tidak bisa mengakses
private variabel dan method.

Example1: Meangakses private variabel dari
class
l llain
i
1. class Complex {
2.
private double real, imaginary;
3.
4.
public Complex(double r, double i) {
5.
real = r; imaginary = i;
6.
}
7
7.
public
bli C
Complex
l
add(Complex
dd(C
l
c)
) {
8.
return new Complex(real + c.real,
9.
imaginary + c.imaginary);
10.
}
11 }
11.
12.
13. class Client {
14.
void useThem() {
15.
Complex c1 = new Complex(1, 2);
16.
Complex c2 = new Complex(3, 4);
17.
Complex c3 = c1.add(c2);
18.
double d = c3.real; // Illegal!
19.
}
20. }

Example2: Mengakses private variabel dari
subclass.
b l
1. class Complex {
2
2.
private double real
real, imaginary;
3. }
4.
5.
6. class SubComplex extends Complex {
7.
SubComplex(double r, double i) {
8.
real = r; // Trouble!
9.
}
10.}

Default
y

y
y

y

Is the name of the access of classes,
variables,
i bl and
d methods,
th d if you don’t
d ’t specify
if
an access modifier.
Bukan merupakan Java keyword.
Semua feature class-class yang ada dalam
satu package bisa diakses oleh semua yang
ada
d dalam
d l package
k
t
tersebut.
b t
Class diluar package boleh melakukan
melakukan subclass, tetapi subclass tersebut
tidak bisa mengakses feature superclass.

Example: default
package sportinggoods;
2. class Ski {
2
3.
void applyWax() { . . . }
access
4
4.
}
1.

Æ default

1. package sportinggoods;
2 class
2.
l
DownhillSki
hill ki extends
d Ski
ki {
3.
void tuneup() {
4.
applyWax();
5.
// other tuneup functionality here
6.
}
7. }

protected
y
y

Protected mempunyai kemampuan akses
yang lebih besar daripada private dan default.
Protected digunakan pada:
◦ Variabel
V b l
◦ Method

y
y

Protected feature dari suatu class bisa
diakses oleh semua class dalam satu package.
Class diluar package boleh melakukan
melakukan subclass, dan subclass tersebut
bisa mengakses
g
feature superclass.
p

Example: protected
1.
//
2.
3.
4.

package adifferentpackage; // Class Ski now in
a different p
package
g
class Ski {
protected void applyWax() { . . . }
}

1. package sportinggoods;
2. class DownhillSki extends Ski {
3
3.
void
oid t
tuneup()
ne p() {
4.
applyWax();
5.
// other tuneup functionality here
6.
}
7. }

Summary of Access Modes
y
y

y

y

public: A public feature may be accessed by any class.
protected: A protected feature may only be
accessed by a subclass of the class that owns the
feature, or by a member of the same package as the
class that owns the feature, or by a member of the
diferent package as the class that owns the feature
default: A default feature may only be accessed by a
cclass
ass from
o tthe same
sa
pac
package
ag as the
t class
c ass that
t at owns
ow s the
t
feature.
private: A private feature may only be accessed by
the class that owns the feature.

Summary of Access Modes to Class
M b
Members

Other Modifiers
y

final, abstract, static, native,
transient, synchronized, and volatile.

Note: transient

covered here.

and volatile are not

final
y

Bisa diaplikasikan
p
pada
p
:
◦ Classes
◦ Methods
◦ Variables.

y

Final features tidak bisa diubah atau
dimodifikasi.

Final: Bila terletak pada variabel
Berarti variabel tersebut bersifat konstan.
y Ketika variabel tersebut mendapat nilai
pertama,
pe
ta a, maka
a a nilai
a tersebut
te sebut ttidak
a dapat
apat
diganti.
y

final int x;
x = 5;
x+ + ;

error !! karena berusaha
mengubah
b h variabel
i b l
constan

Final: Bila terletak pada method
y

Berarti method tersebut tidak dapat
p dioverride.

class Parent {
public final void I nfo() {

}
}
class Child extends Parent {
public void I nfo() {

}
}

error !! karena berusaha
meng-override method
yang final

Final: Bila terletak pada class
y

Berarti class tersebut tidak dapat
p
diturunkan.
final class Parent {

}
class Child extends Parent {

}

error !! karena
berusaha menurunkan
class yang final

Final: Bila digunakan sebagai reference pada
object
bj
y

it is the reference that must stay the same, not the
j .
object

1. class Walrus {
2
2.
int weight;
3.
Walrus(int w) { weight = w; }
4. }
5.
6. class Tester {
7.
final Walrus w1 = new Walrus(1500);
8.
void test() {
9.
w1 = new Walrus(1400); // Illegal
10.
w1.weight = 1800; // Legal
11.
}
12. }

abstract
y

Dapat diaplikasikan pada:
◦ Classes
◦ Methods.

y
y
y
y

y

Abstract class adalah class yang mempunyai setidaknya satu abstract
method.
method
Abstract method adalah method yang tidak berisi apa-apa (hanya
deklarasi method).
Implementasi dari isi abstract method tersebut dilakukan pada class
turunan.
Oleh karena itu, suatu class yang termasuk abstract tidak bisa
y y karena di dalamnya
y terdapat
p abstract method yang
y g
dibuat obyeknya
belum ada isinya.
Konsekuensinya, suatu abstract class haruslah diturunkan dimana
pada class turunan tersebut berisi implementasi dari abstract
method
h d yang ada
d di parent class-nya
l

abstract
y

a class must be declared abstract if:
◦ The class has one or more abstract methods.
◦ The class inherits one or more abstract method (from an
abstract parent) for which it does not provide implementations.
◦ The class declares that it implements an interface but does not
provide implementations for every method of that interface.

static
y

Bisa diaplikasikan pada:
◦ Variabel
◦ Method

Variabel atau method yang dideklarasikan
static akan bisa diakses tanpa harus
terlebih dahulu membuat obyek.
y
y Example:
y

1. class Ecstatic{
2.
static int x = 0;
3.
Ecstatic() { x++; }
4. }

Example: static pada variabel
class Nilai {
public int x = 5;
public static int y = 10;
}
public class Tes {
public static void main(String args[]) {
Nilai nil = new Nilai();
System.out.println(“Nilai x = “ + nil.x);
System.out.println(“Nilai y = “ + Nilai.y);
}
}

Data member y pada class Nilai dapat diakses langsung tanpa harus
membuat obyeknya.
y Hal ini berbeda dengan pengaksesan data member x yang harus
did h l i d
didahului
dengan pembuatan
b t obyek
b kd
darii class
l Nil
Nilai.i
y

Static:Variabel dapat dikategorikan menjadi
2 bagian:
b i
y

instance variabel
adalah variabel yang hanya dapat diakses setelah
obyeknya dibuat. Dengan kata lain, instance variabel
adalah variabel yang dimiliki oleh suatu obyek dari
suatu class.

y

class variabel
adalah variabel yang dapat diakses tanpa perlu adanya
obyek Dengan kata lain,
obyek.
lain class variabel adalah variabel
yang dimiliki oleh suatu class.

Example:
p static pada
p
method
y

Jika suatu method itu dideklarasikan static, maka
semua variabel dari data member yang ada dalam
method tersebut harus juga dideklarasikan static.

Example
1. class SomeClass {
2
2.
static int i = 48;
3.
int j = 1;
4.
5.
public static void main(String args[]) {
6.
i += 100;
7.
// j *= 5; Lucky for us this is
commented out!
8.
}
9. }

Static Initializers
y
y

Is simply surrounded by curly braces and labeled static.
Th are executed
They
d at the
h time that
h class
l StaticExample
S
E
l
is loaded.

1. public
1
bli class
l
StaticExample
i
l {
2.
static double d=1.23;
3.
4
4.
static {
5.
System.out.println(“Static code: d=“ + d++);
6.
}
7.
8.
public static void main(String args[]) {
9.
System.out.println(“main: d = “ + d++);
10.
}
11. }

native
can refer onlyy to methods.
y indicates that the body of a method is to
be found
ou eelsewhere
sew e e outs
outsidee the
t e Java
Virtual Machine (JVM)
y written in a non-Java language
language, typically C
or C++, and compiled for a single target
machine type.
type
y

Example: native
class NativeExample{
p {
native void doSomethingLocal(int i);
static {
S
System.loadLibrary(“MyNativeLib”);
l dLib
(“M N i Lib”)
}
}

Others
transient: applies
pp
onlyy to variables.
y synchronized: used to control access to
ccritical
t ca code
co e in multithreaded
u t t ea e programs.
p og a s.
y volatile: indicates that such variables might
be modified asynchronously
y

Modifiers and Features