Anda di halaman 1dari 17

INFORMATION HIDING, ENCAPSULATION, INHERITANCE DAN INHERITANCE, POLYMORHISM

InformationHidingdanEncapsulation
InformationHiding g adalah menyembunyikan attribute danmethod suatuobjekdariobjeklain. Encapsulation adalahmenyembunyikan attribute suatuobjekdariobjeklain. Attributemaupunmethoddisembunyikan dengancaramemberikanmodifierprivate.

wilis k - IF UPN "V" Yk

InformationHidingdanEncapsulation
methodsetter :methoddalamkelasyang sama,yangmemberikannilai padaattribute private method getter :methodmasihdalamkelas yangsama, sama yangmengambilnilai dari attributeprivate

contohprogramInformationHidingdan Encapsulation
PersegiPanjang.java
public bli class l PersegiPanjang{ P iP j { privatedoublepanjang;//attributeygdihide privatedoublelebar;//attributeygdihide privatedoubletinggi;//attributeygdihide public bli PersegiPanjang() P iP j (){ panjang=0; lebar=0; } private i double d bl luas(double l (d bl p,double d bl l){//diencap returnp*l; } publicvoidsetPanjang(doublepanjang){ this.panjang hi j =panjang; j } publicvoidsetLebar(doublelebar){ this.lebar=lebar; } public double getPanjang() { return panjang; } public double getLebar() { return lebar; } public double getLuas() { return luas(panjang, lebar); } }

MainPersegiPanjang.java

publicclassMainPersegiPanjang{ publicstaticvoidmain(String[]srgs){ PersegiPanjangpp=newPersegiPanjang(); pp.setPanjang(10); ( ) pp.setLebar(20); System.out.println("Panjang y p ( j g:"+pp pp.getPanjang()); g j g()) System.out.println("Lebar:"+pp.getLebar()); System.out.println("Luas:"+pp.getLuas()); } }
Output

Inheritance
Semuaattributedanmethoddarisuatu kelassuperdapatdiwariskankesubkelas Bentukpewarisan:
[modifier]classnamaSubKelasextend namaKelasSuper{ //classBody }

OverrridingAttributedanMethod
Overridingadalahkemampuansuatu subkelasuntukmemodifikasiattributedan methodmilikkelassupernya(bukanprivate ataufinal). Modifikasi:jumlahparameter, parameter tipe parameter,tipereturnvalue,ataupun lingkunganpemrosesandatanya

contohprogramoverriding:
KelasSuper.java
classKelasSuper{ publicvoidmethodAsli(){ System.out.println("Methodmilik KelasSuperjalan"); jalan ); } publicstaticvoidmain(String[]args){ KelasSuperoks=newKelasSuper(); oks.methodAsli(); } }

Output

contohprogramoverriding:
SubKelas.java
classSubKelasextendsKelasSuper{ public bli void idmethodAsli() th dA li(){ System.out.println("Methodygoverridedjalan"); } publicvoidmethodPemanggil(){ System.out.println("MethodpemanggilmethodAslijln"); super.methodAsli();//ygdipanggilmilikkelassuper } publicstaticvoidmain(String[]args){ SubKelasosk=newSubKelas(); osk.methodAsli(); osk.methodPemanggil(); } }

O t t Output

MenggunakanMethoddanConstructorKelas Super Constructor


super(); super(tipeparameter);

Method h d ( (nonstatic) i )
super.namaMethod();

Polymorphism
artinyabersifatpolymorphy(memiliki banyakbentuk) Methodmethodoverloadingdalamkelas yangsama M th dmethod Method th doverloading l di dalam d l kelas k l yangberbeda(kelasturunannya)

contoh1
EkspresiWajah.java
classEkspresiWajah{ p j { publicStringrespons(){ return("Perhatikanekspresiwajahsaya"); } } classGembiraextendsEkspresiWajah{ publicStringrespons(){ return("ha t ("h h haha.."); h ") } } classSedihextendsEkspresiWajah{ publicStringrespons(){ return("hikhikngeeengeeengeee.."); } } class Marah extends EkspresiWajah{ public String respons() { return("Hai return( Hai kurang ajar ajar..! !"); ); } }

MainEkspresiWajah.java
classMainEkspresiWajah{ publicstaticvoidmain(Stringargs[]){ EkspresiWajahobjEkspresi=newEkspresiWajah(); GembiraobjGembira=newGembira(); SedihobjSedih=newSedih(); MarahobjMarah=newMarah(); EkspresiWajah[]arrEkspresi=newEkspresiWajah[4]; arrEkspresi[0]=objEkspresi; arrEkspresi[1]=objGembira; arrEkspresi[2]=objSedih; arrEkspresi[3]=objMarah; System.out.println("Ekspresi[0]:"+arrEkspresi[0].respons()); System.out.println("Ekspresi[1]: l (" k [ ] "+arrEkspresi[1].respons()); " k [ ] ()) System.out.println("Ekspresi[2]:"+arrEkspresi[2].respons()); System.out.println("Ekspresi[3]:"+arrEkspresi[3].respons()); } }

contoh1
Output

Employee.java

contoh2

publicclassEmployee{ privateStringname; privatedoublesalary; protectedEmployee(Stringn,doubles){ name=n; salary=s; } protectedStringgetDetails(){ return"Name:"+name+"\nSalary:"+salary; } publicvoidcetak(){ System.out.println("CobadiEmployee"); } }

Manager.java
publicclassManagerextendsEmployee{ pri ateStrin private Stringdepartment department; publicManager(Stringnama,doublesalary,Stringdep){ super(nama,salary); department=dep; } publicStringgetDepartment(){ returndepartment; } publicStringgetDetails(){ returnsuper.getDetails()+"\nDepartment:"+getDepartment(); } publicvoidcetak(){ System.out.println("CobadiManager"); } }

View.java
publicclassView{ public bli static t ti void idmain(String[] i (St i []args) ) { Employeee=newEmployee("Ali",1200000); Employeeem=newManager("Ali",1200000,"Informatika"); S t System.out.println("Data t i tl ("D t employee l :\n"+e.getDetails()); \ " tD t il ()) System.out.println("Datamanager:\n"+em.getDetails()); em.cetak(); } }

Output