Copyright © 2005-2007 William C. BentonWhere we left off...•We’d developed a calculator application, but many of the classes had very similar method bodies•“Reusing” code via cut-and-paste is tedious and error-prone•Let’s review the situation:1Copyright © 2005-2007 William C. BentonWhat’s wrong with our code?2•Most of the eval methods in the BinaryOp classes look roughly the same:•evaluate left operand, getting a float•evaluate right operand, getting a float•perform some operation on these two floatsCopyright © 2005-2007 William C. BentonWhat’s wrong with our code?2•Most of the eval methods in the BinaryOp classes look roughly the same:•evaluate left operand, getting a float•evaluate right operand, getting a float•perform some operation on these two floats•Most of the eval methods in the BinaryOp classes look roughly the same:•evaluate left operand, getting a float•evaluate right operand, getting a float•perform some operation on these two floatsCopyright © 2005-2007 William C. BentonWhat’s wrong with our code?2•Most of the eval methods in the BinaryOp classes look roughly the same:•evaluate left operand, getting a float•evaluate right operand, getting a float•perform some operation on these two floats•Most of the eval methods in the BinaryOp classes look roughly the same:•evaluate left operand, getting a float•evaluate right operand, getting a float•perform some operation on these two floatsNote that two of these steps are the same for every operation!Copyright © 2005-2007 William C. BentonWhat’s wrong with our code?2•Most of the eval methods in the BinaryOp classes look roughly the same:•evaluate left operand, getting a float•evaluate right operand, getting a float•perform some operation on these two floats•Most of the eval methods in the BinaryOp classes look roughly the same:•evaluate left operand, getting a float•evaluate right operand, getting a float•perform some operation on these two floatsNote that two of these steps are the same for every operation!It would be nice to write these once and reuse them!Copyright © 2005-2007 William C. BentonMotivation: inheritance•Interfaces enable us to write code without knowing what specific classes it uses•Interfaces also enable us to write classes that can be used by code that doesn’t know about them3Copyright © 2005-2007 William C. BentonMotivation: inheritance•However, when we declare an interface, we only specify method names, not method implementations•(Each implementing class provides its own implementations)•Sometimes, it would be nice to reuse method implementations by extending classes4Copyright © 2005-2007 William C. BentonExtending classes5•Often, we would like a class to inherit behavior and fields from another class•This is the case when you have an “is-a” relationship between two classes•#1 critical mistake of novice OO programmers: overusing inheritanceCopyright © 2005-2007 William C. BentonExamples:•A car is a vehicle•A 1985 Yugo is a car (roughly speaking)•A checking account is a bank account•A platypus is a monotreme•A monotreme is a mammal•A mammal is a vertebrate•...6Copyright © 2005-2007 William C. Benton“Inherited” behaviors•Vehicle•abstract concept•Car•adds engine, fuel tank data•ignition, accelerate, brake, steer methods•Yugo•adds fake leather seats7Copyright © 2005-2007 William C. Benton...or wider and narrower types•Vertebrates•Lots of wildly different animals•Mammals•warm-blooded, milk-producing animals•Monotremes•that’s just weird!8Copyright © 2005-2007 William C. BentonWhat does this mean for us?•One Java class can “extend” another•Extending class has all of the fields of the base class•Can add its own; private is still private•Extending class has all of the methods of the base class•Can add, override, or inherit9Copyright © 2005-2007 William C. BentonInheritance is a language feature that allows a derived class to extend a base class.10Copyright © 2005-2007 William C. BentonHow do we declare that a class extends another class?11Copyright © 2005-2007 William C. BentonWe declare that one class extends another class with the extends keyword.12Copyright © 2005-2007 William C. Bentonpublic class MultiClicker extends Clicker { public void click(int k) { for(int i = 0; i < k; i++) // can’t access clicks! click(); } }}13Copyright © 2005-2007 William C. BentonWhat does the derived class “inherit”?14Copyright © 2005-2007 William C. BentonThe derived class inherits all of the fields of the base class and all of its public methods.15(More on fields soon.)Copyright © 2005-2007 William C. Benton16•add methods to the base class,•inherit methods from the base class, or•override methods declared in the base classThe derived class may...Copyright © 2005-2007 William C. Benton17getName()Vehicleignition()steer()accel()brake()MotorVehiclegetName()ignition()steer()accel()brake()Cartow(Vehicle)disableAirBag()TruckremainIronic()YugoattractPolice()ViperVehicle v;// ...v.getName();Copyright © 2005-2007 William C. BentonPolymorphic method dispatch chooses the right method based on the type of the base object.(Just like with interfaces.)(How is this different from overloading?)18Copyright © 2005-2007 William C. BentonYou can explicitly invoke methods or constructors of your superclass with the super keyword.19Copyright © 2005-2007 William C. BentonAny questions?20Copyright © 2005-2007 William C. BentonExample21•Bank accounts are ubiquitous•Almost every kind of bank account allows you to check your balance and deposit and withdraw funds•Other kinds have additional capabilities and restrictionsCopyright © 2005-2007 William C. Bentonpublic class BankAccount { private float balance; public void deposit(float amt) { balance += amt; } public void withdraw(float amt) { balance -= amt; } public float getBalance() { return balance; }}22Copyright © 2005-2007 William C. BentonChecking accounts23•Say we wanted to implement a checking account:•Just like a bank account, but $0.10 fees for every withdrawal if balance goes under $200.•How would we do this?Copyright © 2005-2007 William C. Bentonpublic class BankAccount { private float balance; public void deposit(float amt) { balance += amt; } public void withdraw(float amt) { balance -= amt; }
View Full Document