1 / 12

CSE 1341 Honors

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

gilon
Download Presentation

CSE 1341 Honors

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. CSE 1341 Honors Professor Mark Fontenot Southern Methodist University Note Set 17

  2. 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).

  3. Object Composition • One object contains instances of other objects as data members Car Motor A/C System Braking System

  4. 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

  5. Inheritance

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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(…);

  11. 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; } }

  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

More Related