160 likes | 179 Views
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.
E N D
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. • When we want to add additional features to an existing class.
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
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
Developing an Extended Class • declare the class(es) public class CD extends Item { … public class DVD extends Item { …
Developing an Extended Class • declare the new data … private String artist; private int numberOfTracks; … private String director;
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; }
Developing an Extended Class • adjust the methods • Leave inherited methods alone • Add completely new methods • Modify/Override inherited methods
Developing an Extended Class • Leave inherited methods alone • setComment() and setOwn() are both inherited from Item and there is no need to change them.
Developing an Extended Class • Add completely new methods • We need an accessor method for tracks public void getNumberOfTracks() { return numberOfTracks; }
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…
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)
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.
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.
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!
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.