CMU CS 15214 - Inheritance (20 pages)
Previewing pages 1, 2, 19, 20 of 20 page document View the full content.Inheritance
Previewing pages 1, 2, 19, 20 of actual document.
View the full content.View Full Document
Inheritance
0
0
186 views
- Pages:
- 20
- School:
- Carnegie Mellon University
- Course:
- Cs 15214 - Principles of Software Construction: Objects, Design, and Concurrency
Inheritance Principles of Software System Construction Jonathan Aldrich Bill Scherlis Spring 2012 Bank Accounts What kind of accounts does a bank offer What are their basic features Fall 2011 15 214 Course Introduction 2 Account Interfaces interface CheckingAccount interface SavingsAccount getBalance float deposit amount float withdraw amount float boolean transfer amount float target Account boolean getFee float getBalance float deposit amount float withdraw amount float boolean transfer amount float target Account boolean getInterestRate float What do you think of this design Fall 2011 15 214 Course Introduction 3 Account Type Hierarchy interface Account CheckingAccount extends Account All methods from Account are inherited copied to CheckingAccount getBalance float deposit amount float withdraw amount float boolean transfer amount float target Account boolean monthlyAdjustment interface CheckingAccount getFee float If we know we have a CheckingAccount additional methods are available Fall 2011 SavingsAccount is a subtype of Account Account is a supertype of SavingsAccount interface SavingsAccount getInterestRate float interface InterestCheckingAccount 15 214 Course Introduction Multiple interface extension 4 The Power of Object Oriented Interfaces Polymorphism Different kinds of objects can be treated uniformly by clients Keep a list of all accounts Use accessor getBalance and mutator deposit transfer methods Each object behaves according to its type monthlyAdjustment fee for checking interest for savings void adjustAll for Account acct getAllAccounts acct monthlyAdjustment Impact Add new kind of account client code does not change Important because this kind of change happens often Polymorphism is the key strength of OO Difficult to encode in other paradigms and languages C ML etc Will come back repeatedly in this class Fall 2011 15 214 Course Introduction 5 Implementing Accounts All 3 accounts similar interface Account Same code for balance deposit withdraw transfer Differ in monthlyAdjustment Differ in additional features getBalance float deposit amount float withdraw amount float boolean transfer amount float target Account boolean monthlyAdjustment Duplication is bad Lots of code to type Fix bugs 3 times Make enhancements 3 times Easily becomes inconsistent What if another new account type is developed interface CheckingAccount getFee float interface SavingsAccount getInterestRate float interface InterestCheckingAccount Design mantra once and only once Fall 2011 15 214 Course Introduction 6 Reusing Account Code public abstract class AbstractAccount implements Account protected float balance 0 0 public float getBalance return balance abstract public void monthlyAdjustment other methods an abstract class is interface missing the Account getBalance floatof one implemention deposit amount float or more methods withdraw amount float boolean transfer amount float target Account boolean protected elements monthlyAdjustment are visible in subclasses AbstractAccount interface CheckingAccount an abstract method is left balance float be implemented in a getBalance float getFee to float deposit amount float subclass withdraw amount float boolean transfer amount float target Account boolean monthlyAdjustment public class CheckingAccountImpl extends AbstractAcount implements CheckingAccount public void monthlyAdjustment balance getFee public float getFee fee calculation Fall 2011 CheckingAccountImpl no need to define monthlyAdjustment getBalance the code float is getFee inherited from AbstractAccount 15 214 Course Introduction 7 Inheritance vs Subtyping Inheritance A class reuses code from a superclass class A extends B Inheritance is for code reuse Write code once and only once Code from superclass implicitly available in subclass Subtyping A class implements a Java interface class A implements I A class implements the implicit interface of another class class A extends B both subtyping and inheritance Subtyping is for polymorphism Accessing objects the same way but getting different behavior Subtype is substitutable for supertype Fall 2011 15 214 Course Introduction 8 Challenge Is Inheritance Necessary Can we get the same amount of code reuse using only interfaces interface Account getBalance float deposit amount float withdraw amount float boolean transfer amount float target Account boolean monthlyAdjustment interface CheckingAccount getFee float interface SavingsAccount getInterestRate float interface InterestCheckingAccount Fall 2011 15 214 Course Introduction 9 Reuse via Wrapping interface Account getBalance float deposit amount float withdraw amount float boolean transfer amount float target Account boolean monthlyAdjustment requires a lot of forwarding interface CheckingAccount getFee float CheckingAccountImpl monthlyAdjustment getFee float getBalance float deposit amount float withdraw amount float boolean transfer amount float target Account boolean Fall 2011 basicAcnt BasicAccountImpl balance float basicAcnt getBalance 15 214 Course Introduction getBalance float deposit amount float withdraw amount float boolean transfer amount float target Account boolean 10 Reuse via Wrapping version 2 interface Account getBalance float deposit amount float withdraw amount float boolean transfer amount float target Account boolean monthlyAdjustment BasicAccountImpl requires two way dependence account adjustment balance float doAdjust getBalance float deposit amount float withdraw amount float boolean transfer amount float target Account boolean monthlyAdjustment SavingsAccountAdjustment doAdjust float bal account getBalance float interest bal interestRate account deposit interest adjustment doAdjust Fall 2011 interface Adjustment 15 214 Course Introduction 11 Inheritance vs Delegation The wrapping strategy described above is called delegation Delegation can be cleaner than inheritance Reused code in a separate object Interfaces between objects However inheritance eliminates boilerplate Forwarding functions Recursive dependencies Fall 2011 15 214 Course Introduction 12 Overloading When an object has multiple methods with the same name but different argument types Who can name an example Why would anyone do this Fall 2011 15 214 Course Introduction 13 Method dispatch revisited Example call x foo 5 Step 1 compile time determine what class to look in Look at the static type of the receiver x in the example above Step 2 compile time determine the method signature Find all methods in the
View Full Document