Pemrograman
Pemrograman BerorientasiBerorientasi Pemrograman
Pemrograman BerorientasiBerorientasi Obyek
Obyek
D 7 Day 7 :
Topik Topik :: Topik Topik ::
y Enkapsulasi dan abstraki datap
y Modifier
y Package declarations y Package declarations
y Import statements
A l
y Access control
UML UML UML UML
y What is UML?
y UML = Unified Modeling Language
y UML is a graphical language for modeling y UML is a graphical language for modeling
Information Hiding Information Hiding Information Hiding Information Hiding
Problem :
Information Hiding Information Hiding Information Hiding Information Hiding
MyDate Day : Integer Day : Integer Month : Integer Year : Integer GetDay() GetMonth() GetMonth() GetYear() SetDay() SetMonth() SetYear() ValidDay()y()
Encapsulation Encapsulation Encapsulation Encapsulation
y Menyembunyikan detail implementasi dari kelas y Untuk mengakses data dari kelas harus melewati
interface (method)
y Kode program lebih mudah dikelola y Kode program lebih mudah dikelola
MyDate Day : Integery g Month : Integer Year : Integer
Software Package Software Package Software Package Software Package
y Package dapat membantu dalam mengelola sistem
f b
software yang besar
Contoh Contoh : :
Perusahaan
Perusahaan pengantaranpengantaran (shipping)(shipping) y S/W harus bisa mendukung sebuah perusahaan pengantaran y Perusahaan ini mengelola sejumlah kendaraan yang mengirim y Perusahaan ini mengelola sejumlah kendaraan yang mengirim
paket
y Jumlah berat paket merupakan faktor terpenting dalam pemilihan kendaraan
p
y Perusahaan ini memiliki 2 tipe kendaraan yaitu truk dan angkutan laut
y Paket yang dikirim diukur dalam satuan kilogram, algoritmay g g g
untuk menghitung kekuatan mesin kendaraan yang diperlukan untuk mengangkut paket – paket digunakan satuan Newton
y Terdapat GUI untuk memonitor penambahan paket pada
k d
kendaraan
y Terdapat beberapa laporan mengenai pengiriman paket
Shipping Package
Shipping Package DirektoriDirektori Shipping Package
Shipping Package DirektoriDirektori
y Package disimpan pada pohom direktori
Pembuatan
Pembuatan padapada projectproject Pembuatan
Pembuatan padapada projectproject
JavaProject/
Classes/ History/ Src/ Src/
Domain/ GUI / Report/
Compiling using –d
Modifiers Modifiers Modifiers Modifiers
y Give the compiler information about the nature of
code data or classes code, data, or classes.
y Called access modifiers, dictate which classes are allowed
to use a feature.
y Can be used in combination to describe the attributes
of a feature.
Modifier Overview Modifier Overview Modifier Overview Modifier Overview
y Access Modifiers
◦ publicpublic ◦ protected ◦ private
y Other Modifiers
◦ final ◦ abstract ◦ static ◦ native ◦ transient ◦ synchronized ◦ volatile
The Access Modifiers The Access Modifiers The Access Modifiers The Access Modifiers
y Fungsi: melakukan kontrol terhadap class g p
feature yang menggunakan modifier ini.
y Suatu feature minimal harus memiliki satu Suatu eatu e a a us e satu
modifier.
y Yang termasuk class feature: y Yang termasuk class feature:
◦ The class itself
◦ Its member variables
◦ Its member variables
The Access Modifiers The Access Modifiers The Access Modifiers The Access Modifiers
y Suatu class Yyy mempunyai yy p y akses ke class
Xxx.
y Arti t Æ class Yyy tersebut mampu:c ass yy te sebut a pu:
◦ Membuat instance dari class Xxx.
◦ Melakukan extend terhadap class Xxx.Melakukan extend terhadap class Xxx.
Declaration Declaration Declaration Declaration
y Legal Declaration
◦ class Parser { }
◦ class Parser { ... }
◦ public class EightDimensionalComplex { ... }
◦ private int i;
◦ Graphics offScreenGC;
◦ Graphics offScreenGC;
◦ protected double getChiSquared() { ... }
y Illegal Declaration y Illegal Declaration
◦ public protected int x; // At most 1 access modifier
private private private private
y Digunakan oleh:
b l
◦ variabel
◦ method
y Variabel dan method yang dideklarasikan y Variabel dan method yang dideklarasikan
private hanya bisa diakses oleh instance dari class yg mendeklarasikan variabel dan
th d t b t
method tersebut.
y Private variabel dan method dari class Xxx
hanya bisa diakses melalui (within) class Xxx. hanya bisa diakses melalui (within) class Xxx.
y Instance dari subclass tidak bisa mengakses
Example1: Meangakses private variabel dari Example1: Meangakses private variabel dari
l l i l l i class lain class lain
1. class Complex {
2. private double real, imaginary; 3.
4. public Complex(double r, double i) { 5. real = r; imaginary = i;
6. }
7 bli C l dd(C l ) { 7. public Complex add(Complex 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); 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!
Example2: Mengakses private variabel dari Example2: Mengakses private variabel dari
b l b l
subclass. subclass.
1. class Complex {
2 private double real imaginary; 2. private double real, imaginary; 3. }
4. 5 5.
6. class SubComplex extends Complex {
7. SubComplex(double r, double i) {
8. real = r; // Trouble!
Default Default Default Default
y Is the name of the access of classes,
i bl d th d if d ’t if
variables, and methods, if you don’t specify an access modifier.
y Bukan merupakan Java keyword.Bukan merupakan Java keyword.
y Semua feature class-class yang ada dalam
satu package bisa diakses oleh semua yang
d d l k t b t
ada dalam package tersebut.
y Class diluar package boleh melakukan
Example: default Example: default Example: default Example: default
1. package sportinggoods; 2 class Ski {
2. class Ski {
3. void applyWax() { . . . } Æ default
access
4 } 4. }
1. package sportinggoods;
2 l hill ki d ki {
2. class DownhillSki extends Ski { 3. void tuneup() {
4. applyWax();
5. // other tuneup functionality here 6. }
protected protected protected protected
y Protected mempunyai kemampuan akses
yang lebih besar daripada private dan default.
y Protected digunakan pada:
V b l
◦ Variabel
◦ Method
y Protected feature dari suatu class bisa y Protected feature dari suatu class bisa
diakses oleh semua class dalam satu package.
y Class diluar package boleh melakukan y Class diluar package boleh melakukan
Example: protected Example: protected Example: protected Example: protected
1. package adifferentpackage; // Class Ski now in // a different package
// p g
2. class Ski {
3. protected void applyWax() { . . . } 4. }
1. package sportinggoods;
2. class DownhillSki extends Ski { 3 oid t ne p() {
3. void tuneup() { 4. applyWax();
5. // other tuneup functionality here
6. }
Summary of Access Modes Summary of Access Modes Summary of Access Modes Summary of Access Modes
y public: A public feature may be accessed by any class.
y protected: A protected feature may only be
y 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 class that owns the feature, or by a member of the diferent package as the class that owns the feature
y default: A default feature may only be accessed by a class from the same package as the class that owns the c ass o t sa pac ag as t c ass t at ow s t feature.
Summary of Access Modes to Class Summary of Access Modes to Class
M b
M b
Other Modifiers Other Modifiers Other Modifiers Other Modifiers
y final, abstract, static, native,
transient, synchronized, and volatile.
Note: transient and volatile are not
final final final final
y Bisa diaplikasikan pada :p p
◦ Classes
◦ Methods
◦ Variables.
y Final features tidak bisa diubah atau Final features tidak bisa diubah atau
Final: Bila terletak pada variabel Final: Bila terletak pada variabel Final: Bila terletak pada variabel Final: Bila terletak pada variabel
y Berarti variabel tersebut bersifat konstan.
y Ketika variabel tersebut mendapat nilai
pertama, maka nilai tersebut tidak dapat pe ta a, a a a te sebut t a apat diganti.
final int x; x = 5;
Final: Bila terletak pada method Final: Bila terletak pada method Final: Bila terletak pada method Final: Bila terletak pada method
y Berarti method tersebut tidak dapat di-p
override.
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 Final: Bila terletak pada class Final: Bila terletak pada class Final: Bila terletak pada class
y Berarti class tersebut tidak dapat p
diturunkan.
final class Parent { … }}
class Child extends Parent { …
}
error !! karena
Final: Bila digunakan sebagai reference pada Final: Bila digunakan sebagai reference pada
bj bj
object object
y it is the reference that must stay the same, not the
objectj .
1. class Walrus { 2 int weight; 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 9. w1 new Walrus(1400); // Illegal 10. w1.weight = 1800; // Legal
abstract abstract abstract abstract
y Dapat diaplikasikan pada:
◦ Classes ◦ Classes ◦ Methods.
y Abstract class adalah class yang mempunyai setidaknya satu abstract method
method.
y Abstract method adalah method yang tidak berisi apa-apa (hanya deklarasi method).
y Implementasi dari isi abstract method tersebut dilakukan pada class
y Implementasi dari isi abstract method tersebut dilakukan pada class turunan.
y Oleh karena itu, suatu class yang termasuk abstract tidak bisa
dibuat obyeknyay y karena di dalamnya terdapat abstract method yang y p y g belum ada isinya.
y Konsekuensinya, suatu abstract class haruslah diturunkan dimana pada class turunan tersebut berisi implementasi dari abstract
h d d di l
abstract abstract abstract 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.
static static static static
y Bisa diaplikasikan pada:
◦ Variabel
◦ Method
y Variabel atau method yang dideklarasikan y Variabel atau method yang dideklarasikan
static akan bisa diakses tanpa harus terlebih dahulu membuat obyek.y
y Example:
1. class Ecstatic{
2. static int x = 0;
Example: static pada variabel Example: static pada variabel Example: static pada variabel Example: static pada variabel
class Nilai {
public int x = 5;
public static int y = 10; }
public class Tes { 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); }
}
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 b t b k d i l Nil i
Static: Variabel dapat dikategorikan menjadi Static: Variabel dapat dikategorikan menjadi 2 b i
2 b i
2 bagian: 2 bagian:
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 adalah variabel yang dimiliki oleh suatu obyek dari suatu class.
y class variabel
Example: static pada method Example: static pada methodpp pp
y Jika suatu method itu dideklarasikan static, maka
Example Example Example Example
1. class SomeClass {
2 static int i = 48; 2. static int i 48; 3. int j = 1;
4.
5. public static void main(String args[]) { 5. public static void main(String args[]) { 6. i += 100;
7. // j *= 5; Lucky for us this is commented out!
Static
Static InitializersInitializers Static
Static InitializersInitializers
y Is simply surrounded by curly braces and labeled static.
Th d h h l S E l
y They are executed at the time that class StaticExample
is loaded.
1 bli l i l { 1. public class StaticExample { 2. static double d=1.23; 3.
4 static { 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. }
native native native native
y can refer only to methods.y
y indicates that the body of a method is to
be found elsewhere outside the Java be ou e sew e e outs e t e Java Virtual Machine (JVM)
y written in a non-Java language typically C y written in a non-Java language, typically C
or C++, and compiled for a single target machine type
Example: native Example: native Example: native Example: native
class NativeExample{p {
native void doSomethingLocal(int i);
static {
S l dLib (“M N i Lib”)
System.loadLibrary(“MyNativeLib”); }
Others Others Others Others
y transient: applies only to variables.pp y
y synchronized: used to control access to
critical code in multithreaded programs. c t ca co e u t t ea e p og a s.
y volatile: indicates that such variables might