120 likes | 216 Views
CSE 1341 Honors. Professor Mark Fontenot Southern Methodist University Note Set 17. Software Reuse. DO NOT reinvent the wheel! If you have source code that works and has been tested, Re-use it ! Simple Example: Methods Performs a task
E N D
CSE 1341 Honors Professor Mark Fontenot Southern Methodist University Note Set 17
Software Reuse • DO NOT reinvent the wheel! • If you have source code that works and has been tested, Re-use it! • Simple Example: • Methods • Performs a task • Can accept input to “change” the way the method executes (parameters or overloading).
Object Composition • One object contains instances of other objects as data members Car Motor A/C System Braking System
Object Composition public class Car { private Motor mSys; private ACSystemacSys; private BrakingSystem brakes; public Car () { mSys = new Motor(); acSys = new ACSystem(); brakes = new BrakingSystem(); } public void start() { mSys.crank(); acSys.previousSettings(); } } Instance Variables can be objects Here is where the contain objects are constructed Car Motor A/C System Braking System Can access various methods from contained objects
Clock • Scenario – You’ve designed a clock class. You’ve tested it and it works well. Clock • hours : int • minutes: int • seconds: int +setTime(h:int, m:int, s:int) +getTime() : String
Alarm Clock • Now you need an alarm clock. Its similar to a clock but needs to also store an alarm time. • You could start again from scratch But that would be silly… AlarmClock • hours : int • minutes: int • seconds: int • alHour : int • alMin : int • alSec : int +setTime(h:int, m:int, s:int) +setAlarmTime(h:int, m:int, s:int) +getTime() : String +getAlarmTime() : String
Alarm Clock is a Clock Clock Superclass • Can implement AlarmClockso that it inheritsall of the data and functionality from from Clock • An object of type AlarmClockaugments the functionality of Clock. • hours : int • minutes: int • seconds: int +setTime(h:int, m:int, s:int) +getTime() : String AlarmClock Subclass • alHour : int • alMin : int • alSec : int +setAlarmTime(h:int, m:int, s:int) +getAlarmTime() : String
Alarm Clock is a Clock Clock public class Clock { private int hours; private int minutes; private int seconds; //implementation of instance //methods for Clock here } public class AlarmClockextends Clock { private intalHour; private intalMin; private intalSec; //instance methods for AlarmClock //Here } • hours : int • minutes: int • seconds: int +setTime(h:int, m:int, s:int) +getTime() : String AlarmClock • alHour : int • alMin : int • alSec : int indicates that AlarmClock is inheriting from Clock. +setAlarmTime(h:int, m:int, s:int) +getAlarmTime() : String
Subclass and Superclasses • Subclass can override the functionality of a method in a superclass • subclass provides a new implementation of the function • must have exactly the same function signature • can call the superclass version of the method by using super.methodName(…);
Example: Employee public class Employee { private String id; private double baseSalary; public Employee () { id = "00000000"; baseSalary = 0.00; } public void setID(String temp) { id = temp; } public void setBaseSalary(doubled) { baseSalary = d; } public String getId() { return id; } public double calcMonthlySalary () { return baseSalary / 12; } }
Example: BasePlusCommission public class BasePlusCommission extends Employee { private double commissionAmt; public void setCommissionAmt (double d) { commissionAmt = d; } public double calcMonthlySalary () { double temp = super.calcMonthlySalary (); temp += commissionAmt / 12; return temp; } } calls the superclass version of the same method Overrides the functionality of same method from base class