1 / 16

Session 18

Session 18. Introducing Inheritance. When do we use inheritance?. When we want to reduce code duplication by pulling together common code from two or more classes. When we want to set up a mechanism for substitutability between two or more classes.

wali
Télécharger la présentation

Session 18

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. Session 18 Introducing Inheritance

  2. When do we use inheritance? • When we want to reduce code duplication by pulling together common code from two or more classes. • When we want to set up a mechanism for substitutability between two or more classes. • When we want to add additional features to an existing class.

  3. 1) Reducing Code Duplication • Create a new parent class which contains all of the “common” code from your set of existing classes • We looked at what both DVD and CD had in common (title, running time, comments, got it) and pulled those variables and accessor/mutator methods into a new class called Item • Next, we have DVD and CD EXTEND Item

  4. Developing an Extended Class (a class which inherits from another) • There are typically four steps in developing an extended class. • declare the class • declare the new data • create the constructors   • adjust the methods if needed

  5. Developing an Extended Class • declare the class(es) public class CD extends Item { … public class DVD extends Item { …

  6. Developing an Extended Class • declare the new data … private String artist; private int numberOfTracks; … private String director;

  7. Developing an Extended Class • create the constructor public CD(String theTitle, String the Artist, int tracks, int time) { super(theTitle, time); artist = theArtist; numberOfTracks= tracks; }

  8. Developing an Extended Class •  adjust the methods • Leave inherited methods alone • Add completely new methods • Modify/Override inherited methods

  9. Developing an Extended Class •   Leave inherited methods alone • setComment() and setOwn() are both inherited from Item and there is no need to change them.

  10. Developing an Extended Class • Add completely new methods • We need an accessor method for tracks public void getNumberOfTracks() { return numberOfTracks; }

  11. Developing an Extended Class • Modify/Override inherited methods • print() is inherited, but it doesn’t really do what we want it to do. • Let’s come back to this in a minute…

  12. 2) Set up substitutability • You can use different data types in a single location if they are all substitutable for the same thing. • A subclass is always substitutable for it’s superclass • Therefore, we can adapt Database to handle a single ArrayList of Items (or things substitutable for Items)

  13. 3) Adding Behavior to a Class • Any time that we need to add behavior to a class we have at least three options: • Add code to the class itself, keeping the original class. • Copy all the old code into a new class and add code to this new class. • Create a subclass that extends the original class' behavior.

  14. Pros and Cons “Add code to the class itself, keeping the original class. “ • Pros: Quick. Convenient. Simple. • Cons: May change the behavior of the class. Thus, it isn’t always an option.

  15. Pros and Cons “Copy all the old code into a new class and add code to this new class. “ • Pros: Quick. Convenient. Simple. • Cons: Duplicated code. Error trap! Error trap!

  16. Pros and Cons “Create a subclass that extends the original class' behavior.“ • Pros: Doesn’t break existing code. Virtually eliminates duplicate code. Provides the most flexibility. • Cons: Slightly more time consuming.

More Related