1 / 22

Enhancing Multimedia Database Structure Using Inheritance

Implementing inheritance in a multimedia database program for efficient code management and extensibility. Learn the advantages, structure improvement, and Java syntax practices for a more organized and versatile application development.

jpage
Download Presentation

Enhancing Multimedia Database Structure Using 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. Chapter 8 Improving Structure with Inheritance

  2. The DoME Example • The Database of Multimedia Entertainment • We will be storing information about CDs and videos • We will want to be able to perform actions on each of these lists of items.

  3. Desired Functionality • Enter information about CDs and videos • Store information permanently • Search functionality • Print lists of all CDs or videos

  4. Title of album Artist Number of tracks Total Playing time “Got it” flag Comment Title of video Name of director Total Playing time “Got it” flag Comment Desired Information

  5. Design Discussion • Each of the private fields would have a setter and a getter • The each list of CDs and videos could be stored in a separate array list • Do you see any issues with this approach? • DoME Source Code

  6. Major Issue • Code duplication • There is code duplication in almost every aspect of every class • With the two ArrayLists we have to duplicate every method. • With repeated information we have to repeat the same code in both the CD and video class • What happens when we want to add books?

  7. Inheritance • Inheritance allows us to define one class as an extension of another • Inheritance allows subclasses to get all the fields and methods from the superclass. • In this way, we can put all the common information in one class and inherit from it • Then we can put the specialized information in the subclasses

  8. Better Classes for DoME

  9. Inheritance Hierarchies

  10. Inheritance and Java • There are some syntax issues when dealing with Java and inheritance • We use the keyword extends to indicate that one class inherits from another class • We may choose to use different access modifiers within the base class to allow for easier access in the child classes • We need ways to initialize the super class

  11. public class Item { private String title; private int playingTime; private boolean gotIt; private String Comment; … } public class CD extends Item { private String artist; private int numberOfTracks; … } Inheritance and Java

  12. Inheritance and Access Rights • Just like any other class a subclass of a superclass is not allowed to access any private data; it must use setters and getters • It does not need to use a dot in front of any methods • It can call them as if they were their own • We will come back to this issue when later and introduce a protected access modifier

  13. Inheritance and Initialization • When we create an object the constructor is called and the state of the object is set. • But how do we set the state of the superclass? • In the first line of the subclass’s constructor we use the key word super and supply any necessary parameters to the superclass’s constructor

  14. public class Item { … public Item( String theTitle, int time ) { title = theTitle; playingTime = time; gotIt = false; comment = “”; } … } public class CD extend Item { … public CD( String theTitle, String theArtist, int tracks, int time ) { super(theTitle, time); artist = theArtist; numberOfTracks = tracks; } … } Example

  15. Things to Notice • The constructor for the CD receives the information necessary to create an item in addition to the information need to create the CD • The key word super is the call to Item’s constructor and we pass in the information it wants • The first line of the subclass’s constructor must always be to the superclass’s constructor through the key word super

  16. Advantages of Inheritance • Avoiding code duplication • Code reuse • Easier maintenance • Extensibility

  17. Subtyping • Let’s take a look at the Database code since we have changed to inheritance • DoME Example V2 • It is much simpler • The reason is subtyping

  18. Subtyping Again • When we have said in the past that when you call a method, the actual parameters must match the type of the formal parameters • We could have said must match the type or be a subtype of the formal paramaters • We can now have one ArrayList that will hold items • So addItem will take an item or any of its subclasses as a parameter.

  19. Subtyping and Assignments • We are allowed to assign to a variable any object that matches its type or is a subtype of its type • These are allowed • Vehicle v1 = new Vehicle(); • Vehicle v2 = new Car(); • Vehicle v3 = new Bike(); • This is not • Car c1 = new Vehicle();

  20. Polymorphism • Polymorphism, for now, is an idea that allows a variable to be able to be more than one thing at different times. • It allows us to simplify our print code for the database. • We will come back to this important idea later

  21. The Object class • In Java all objects inherit from the Object class. • We don’t have to do anything, it is automatic. • This is how ArrayLists can hold any type of object. • They expect an Object as their parameters. • Since all objects inherit from this common base, we can pass any object to a collection in Java • That is also why, it gives us an object back and we have to cast it back to its particular type.

  22. HW • Chapter 8: 8.3, 8.6, 8.11, 8.13, 8.17

More Related