~:Catatan Belajar MazHel:~

implementasi abstract class

Posted in java by m42h31 on 20 Juni 2008

/* latihan implementasi abstract class untuk design pattern  */

public abstract class kendaraan{
public abstract void operasi();
}

class Maju extends kendaraan{
private Mobil mobil;

public Maju(Mobil mobil){
this.mobil=mobil;
}

public void operasi(){  //override method operasi di class kendaraan
this.mobil.setMaju();

}

}


class Mundur extends kendaraan{
private Mobil mobil;

public Mundur(Mobil mobil){
this.mobil=mobil;
}

public void operasi(){  //override method operasi di class kendaraan
this.mobil.setMundur();

}

}

class Mobil {
private String jenis;
private String status;

public Mobil(){
}
public final void setJenis(String jns){
jenis=jns;
}
public final void setStatus(String stt){
status=stt;
}
public final String getJenis(){
return jenis;
}
public final String getStatus(){
return status;
}
public void setMaju(){
this.setStatus(this.getJenis()+” sedang maju..”);
}
public void setMundur(){
this.setStatus(this.getJenis()+” sedang mundur..”);
}

public String toStr(){
return (“Jenis Mobil : “+this.getStatus());
}

}
class DrvMobil{
public static void main (String []args){
Mobil kijang=new Mobil();
Mobil jazz=new Mobil();

kijang.setJenis(“Kijang”+” Family Car”);
jazz.setJenis(“Jazz”+” City Car”);

System.out.println(kijang.getJenis());
System.out.println(jazz.getJenis());

kendaraan kjgMj=new Maju(kijang);
kendaraan jzzMj=new Maju(jazz);

kendaraan kjgMr=new Mundur(kijang);
kendaraan jzzMr=new Mundur(jazz);

kjgMj.operasi();
jzzMj.operasi();

System.out.println(“maju keduanya nih”);
System.out.println(kijang.toStr());
System.out.println(jazz.toStr());

kjgMr.operasi();
jzzMr.operasi();

System.out.println(“mundur keduanya nih”);
System.out.println(kijang.toStr());
System.out.println(jazz.toStr());

kjgMj.operasi();
jzzMr.operasi();
System.out.println(“satu maju satu mundur”);
System.out.println(kijang.toStr());
System.out.println(jazz.toStr());

}
}

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: