1 / 21

Inheritance in collections Week 16

Inheritance in collections Week 16. 3.0. Main concepts to be covered. Inheritance in ArrayList objects Subtyping Substitution. The DoME example. "Database of Multimedia Entertainment" stores details about CDs and DVDs CD: title, artist, # tracks, playing time, got-it, comment

Télécharger la présentation

Inheritance in collections Week 16

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. Inheritance in collectionsWeek 16 3.0

  2. Main concepts to be covered • Inheritance in ArrayList objects • Subtyping • Substitution

  3. The DoME example "Database of Multimedia Entertainment" • stores details about CDs and DVDs • CD: title, artist, # tracks, playing time, got-it, comment • DVD: title, director, playing time, got-it, comment • allows details of items to be printed

  4. DoME objects

  5. DoME classes top half shows fields bottom half shows methods

  6. DoME object model

  7. Class diagram

  8. Database source code public class Database { private ArrayList<CD> cds; private ArrayList<DVD> dvds; public Database() { cds = new ArrayList<CD>(); dvds = new ArrayList<DVD>(); } public void addCD(CD theCD) { cds.add(theCD); } public void addDVD(DVD theDVD) { dvds.add(theDVD); }

  9. Database source code public void list() { for(CD cd : cds) { cd.print(); System.out.println(); } for(DVD dvd : dvds) { dvd.print(); System.out.println(); } }

  10. Critique of DoME • code duplication in CD and DVD classes • also code duplication in Database class • makes maintenance difficult/more work • introduces danger of bugs through incorrect maintenance

  11. Using inheritance

  12. private ArrayList<CD> cds; private ArrayList<DVD> dvds; public Database() { cds = new ArrayList<CD>(); dvds = new ArrayList<DVD>(); } Database source code Old private ArrayList<Item> items; public Database() { items = new ArrayList<Item>(); } New

  13. public void addCD(CD theCD) { cds.add(theCD); } public void addDVD(DVD theDVD) { dvds.add(theDVD); } Database source code Old public void addItem(Item theItem) { items.add(theItem); } New

  14. Database source code public void list() { for(CD cd : cds) { cd.print(); System.out.println(); } for(DVD dvd : dvds) { dvd.print(); System.out.println(); } } Old public void list() { for(Item item : items) { item.print(); System.out.println(); } } New

  15. First, we had: public void addCD(CD theCD) public void addDVD(DVD theDVD) Now, we have: public void addItem(Item theItem) We call this method with either a CD object or a DVD object as a parameter Subtyping

  16. Subclasses and subtyping • Classes define types. • Subclasses define subtypes. • Objects of subclasses can be used where objects of supertypes are required.(This is called substitution.)

  17. Subtyping and assignment subclass objects may be assigned to superclass variables Vehicle v1 = new Vehicle(); Vehicle v2 = new Car(); Vehicle v3 = new Bicycle();

  18. Subtyping and parameter passing public class Database { public void addItem(Item theItem) { ... } } DVDdvd = new DVD(...); CD cd = new CD(...); database.addItem(dvd); database.addItem(cd); subclass objects may be passed to superclass parameters

  19. Object diagram

  20. Class diagram

  21. Review • Inheritance can be used in collections • Variables can hold subtype objects • Subtypes can be used wherever supertype objects are expected (substitution)

More Related