KEMBAR78
P 3 object_oriented_programming | PDF
Object Oriented Programming
Irfan Wahyudin, S.Si, M.Kom
Pertemuan Sebelumnya...
● Top down/bottom up Programming
● Structured Programming
● Procedural Programming
Mengapa Perlu OOP?
● Top-Down/Bottom-Up Programming→
Spaghetti Code
● Structured Programming → Tidak reusable
● Procedural Programming → Sudah reusable,
tetapi belum merepresentasikan object, belum
modular, dan hard-to-manage
Object Oriented Programming
● Untuk itu dikembangkan OOP
● Konsep dasar OOP:
– Objek
– Abstraksi
– Enkapsulasi
– Polimorfisme
– Inheritance
Enkapsulasi
● Ilustrasi...
Kenapa Butuh Enkapsulasi?
Representasi dalam Program
Class Animal{
string color;
bool canWrite;
Animal(bool _canWrite){
canRun = _canWrite;
}
void setColor(string _color){
color = _color;
}
void write(){
if (canWrite){
System.out.println(“Wow...it can write”);
}
}
}
Class AnimalTest{
public static void main(string [] Args){
Animal dog = new Animal(true);
Animal sheep = new Animal(false);
dog.write();
dog.color = “red”;
dog.setColor(“red”);
}
}
Contoh lain...
class Saving {
public class Account {
private decimal accountBalance = 5000;
private decimal registrationFee = 200;
public void deposit(decimal amount) {
amount = amount - registrationFee;
accountBalance += amount;
}
}
static void Main() {
Account myAccount = new Account();
MyAccount.accountBalance += 2000;
decimal myBalance = myAccount.deposit(2000);
}
}
Access Modifiers
● Private
● Protected
● Public
Private
●
Definition: “The type or member can be accessed only by code in
the same class or struct.”
●
Contoh:
Class Mahasiswa(){
private string nik;
private string nama;
private string getNama(){
return nama;
}
}
Class Universitas(){
public static void main(String [] args){
Mahasiswa mhs = new Mahasiswa();
System.out.print(mhs.nama);
}
}
Public
● Definition: “The type or member can be
accessed by any other code in the same
assembly or another assembly that references
it.”
Protected
● Definition: “The type or member can be
accessed only by code in the same class or
struct, or in a class that is derived from that
class.”
● Penggunaan: Lebih banyak pada kasus
turunan class (Inheritance)

P 3 object_oriented_programming

  • 1.
    Object Oriented Programming IrfanWahyudin, S.Si, M.Kom
  • 2.
    Pertemuan Sebelumnya... ● Topdown/bottom up Programming ● Structured Programming ● Procedural Programming
  • 3.
    Mengapa Perlu OOP? ●Top-Down/Bottom-Up Programming→ Spaghetti Code ● Structured Programming → Tidak reusable ● Procedural Programming → Sudah reusable, tetapi belum merepresentasikan object, belum modular, dan hard-to-manage
  • 4.
    Object Oriented Programming ●Untuk itu dikembangkan OOP ● Konsep dasar OOP: – Objek – Abstraksi – Enkapsulasi – Polimorfisme – Inheritance
  • 5.
  • 6.
  • 7.
    Representasi dalam Program ClassAnimal{ string color; bool canWrite; Animal(bool _canWrite){ canRun = _canWrite; } void setColor(string _color){ color = _color; } void write(){ if (canWrite){ System.out.println(“Wow...it can write”); } } } Class AnimalTest{ public static void main(string [] Args){ Animal dog = new Animal(true); Animal sheep = new Animal(false); dog.write(); dog.color = “red”; dog.setColor(“red”); } }
  • 8.
    Contoh lain... class Saving{ public class Account { private decimal accountBalance = 5000; private decimal registrationFee = 200; public void deposit(decimal amount) { amount = amount - registrationFee; accountBalance += amount; } } static void Main() { Account myAccount = new Account(); MyAccount.accountBalance += 2000; decimal myBalance = myAccount.deposit(2000); } }
  • 9.
  • 10.
    Private ● Definition: “The typeor member can be accessed only by code in the same class or struct.” ● Contoh: Class Mahasiswa(){ private string nik; private string nama; private string getNama(){ return nama; } } Class Universitas(){ public static void main(String [] args){ Mahasiswa mhs = new Mahasiswa(); System.out.print(mhs.nama); } }
  • 11.
    Public ● Definition: “Thetype or member can be accessed by any other code in the same assembly or another assembly that references it.”
  • 12.
    Protected ● Definition: “Thetype or member can be accessed only by code in the same class or struct, or in a class that is derived from that class.” ● Penggunaan: Lebih banyak pada kasus turunan class (Inheritance)