1 / 19

Composition & Inheritance

Composition & Inheritance. Viska Mutiawani , M.Sc. Konsep Penting. Composition Inheritance ( pewarisan ) Superclass (super kelas ) Subclass (sub kelas ). Reuse Class. Ada 2 cara untuk reuse ( menggunakan kembali ) class yang telah dibuat : Composition (has-a) Inheritance (is-a).

navid
Download Presentation

Composition & Inheritance

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Composition & Inheritance ViskaMutiawani, M.Sc

  2. KonsepPenting • Composition • Inheritance (pewarisan) • Superclass (super kelas) • Subclass (sub kelas)

  3. Reuse Class • Ada 2 carauntuk reuse (menggunakankembali) class yang telahdibuat: • Composition (has-a) • Inheritance (is-a)

  4. Composition vs Inheritance • Composition : aggregation • Composition : class barudisusundari class yang telahada. • Inheritance : pewarisan • Inheritance : class barudibuatberdasarkanmodifikasidari class yang telahada.

  5. Composition • Contohada class Point. • Anggapkitaakanbuat class Line. Kita dapat re-use class Point dengan composition. • Kita sebut: "A line is composed of two points", or "A line has two points". • Composition jugadisebutrelasi "has-a”

  6. Contoh Composition

  7. Inheritance • Ataupewarisan • AnggapsepertihirarkibinatangdalamBiologi.

  8. Inheritance • Superclass: Class yang beradapadahirarkilebihatas. (Parent) • Memilikivariabeldan method yang umum • Subclass: Class yang beradapadahirarkilebihbawah. (Child) • Memilikivariabeldan method yang lebihkhusus. • Subclass mewarisisemuavariabeldan method darisuperclass-superclassnya.

  9. Inheritance • Kegunaan inheritance: • Menghindariduplikasi code • Reuse code • Menyederhanakan code • Mempermudah maintenance danperluasan code • Inheritance jugadisebutrelasi“is-a” • Contoh: Poodle is a dog • Owl is a bird • Bird is a animal

  10. Contoh Inheritance • Cara subclass mewarisisuperclass: dengan keyword “extends”

  11. Keyword “super” • Keyword superdigunakanuntukmerujukpadavariabeldan method padasuperclass. • Pada constructor, superdigunakanuntukmemanggil constructor darisuperclass.

  12. Constructor pada Inheritance • Constructor subclass di Java tidakdiwarisidarisuperclassnya. • Namun constructor superclassdapatdigunakandidalam subclass denganmenggunakan keyword super(). • Setiap class di Java memilikiconstructornyasendiri.

  13. Method overriding • Selaindarimewarisisemuavariabeldan method darisuperclass-superclassnya, subclass dapatmengoverridevariabeldan method. • Andaikankitainginmenghitungluaspermukaan Cylinder. Kita gunakan method getArea() pada Circle dengancarameng-overridenya. • Dan selanjutnyakitaubahgetVolume() untukmenghitung volume Cylinder.

  14. @Override • @Override disebutsebagaiannotation, yang menyuruh compiler untukmengecekapakahadanama method padasuperclass. • Jikatanpa @Override, method ToString() akandianggapsebagai method baru. • Jikadengan @Override, akanmuncul error kompilasijikakitatulisToString() • Optional but nice to have.

  15. Single Inheritance • Java tidakmensupport multiple inheritance (subclass punyalebihdarisatusuperclass) • Pada Java, subclass hanyabolehpunyasatusuperclasslangsungdiatasnya. Namunsuperclassbolehpunyabanyak subclass.

  16. java.lang.Object • Setiap class pada Java merupakan subclass dari Object (java.lang.Object). • Java.lang.Objectdisebutcommon root. • Hal inidiperlukanuntuk running program dengan JRE, contohnyapadakasus multithreading, garbage collector.

  17. Composition vs Inheritance • “A line is composed of 2 points” vs “A line is a point extended by another point”? • Which one? • Pendapatahli: Use composition if possible, before considering inheritance.

  18. Latihan

  19. Latihan • Rectangle • length : double • width : double • Color : String • + Rectangle() • + Rectangle(length: double, width: double) • + getLength() : double • + getWidth() : double

More Related