Chapter 11 – Interfaces and PolymorphismChapter GoalsInterfaces and PolymorphismConcept #1: InterfacesUsing Interfaces for Code ReuseSlide 6Slide 7ExampleSlide 9Slide 10Slide 11InterfacesSlide 13Slide 14Slide 15Slide 16Slide 17Static constant fieldsSlide 19Slide 20Slide 21ImplementsDefining interfacesImplementing interfacesSlide 25Converting Between Classes and InterfacesConvertingSlide 28Type CastingSlide 30Slide 31PolymorphismPurpose of PolymorphismSlide 34Chapter 11 – Chapter 11 – Interfaces and Interfaces and PolymorphismPolymorphismChapter GoalsChapter GoalsLearn about interfacesLearn about interfacesConvert between class and interface Convert between class and interface referencesreferencesUnderstand the concept of Understand the concept of polymorphismpolymorphismUnderstand the purpose of interfaces to Understand the purpose of interfaces to decouple classesdecouple classesInterfaces and Interfaces and PolymorphismPolymorphismInterfaces are important for Interfaces are important for developing reusable software developing reusable software componentscomponentsPolymorphism is the principal at the Polymorphism is the principal at the heart of this process – a key heart of this process – a key component of object oriented component of object oriented programmingprogrammingConcept #1: InterfacesConcept #1: InterfacesReal world – surface between two Real world – surface between two adjacent entities, spheres, etc.adjacent entities, spheres, etc.CS world – the surface of a classCS world – the surface of a classHow will code behave (what methods How will code behave (what methods will it define)will it define)Ex. Javadocs are an Ex. Javadocs are an interfaceinterface to the to the underlying class – defines how the class underlying class – defines how the class behavesbehavesUsing Interfaces for Using Interfaces for Code ReuseCode ReuseUse Use interface typesinterface types to make code to make code more generalmore generalIdentify common/essential operationsIdentify common/essential operationsLet’s say there is a class Let’s say there is a class DataSetDataSet that that keeps track of a running total of real keeps track of a running total of real numbersnumberspublic class DataSet{public class DataSet{private double sum; private double sum; private double maximum; private double maximum; private int count;private int count;public DataSet(){ public DataSet(){ sum = 0; sum = 0; count = 0; count = 0; maximum = 0; maximum = 0; } } /** /** Adds a data value to the data set Adds a data value to the data set */ */ public void add(double x){ public void add(double x){ sum = sum + x;sum = sum + x;if (count == 0 || maximum < x) if (count == 0 || maximum < x) maximum = x; maximum = x; count++; count++; } }/** /** Gets the average of the added data. Gets the average of the added data. @return the average or 0 if no data has been added @return the average or 0 if no data has been added */*/public double getAverage(){public double getAverage(){if (count == 0) if (count == 0) return 0; return 0; else else return sum / count; return sum / count; }}/** /** Gets the largest of the added data.Gets the largest of the added data.@return the maximum or 0 if no data has been added @return the maximum or 0 if no data has been added */ */ public double getMaximum(){ public double getMaximum(){ return maximum;return maximum;} } } }ExampleExampleProblem: Only works for numbersProblem: Only works for numbersWhat if we wanted to keep track of What if we wanted to keep track of BankAccounts?BankAccounts?public class DataSet{public class DataSet{ . . . . . . public void add(BankAccount x) { public void add(BankAccount x) { sum = sum + x.getBalance(); sum = sum + x.getBalance(); if (count == 0 || maximum.getBalance() < if (count == 0 || maximum.getBalance() < x.getBalance()) x.getBalance())maximum = x;maximum = x;count++;count++;}}public BankAccount getMaximum(){public BankAccount getMaximum(){return maximum;return maximum;}}private double sum;private double sum;private BankAccount maximum;private BankAccount maximum;private int count; private int count; } }ExampleExampleWhat if we want to do the same for What if we want to do the same for Coins?Coins?public class DataSet{public class DataSet{ . . . . . . public void add(Coin x) { public void add(Coin x) { sum = sum + x.getValue(); sum = sum + x.getValue(); if (count == 0 || maximum.Value() < if (count == 0 || maximum.Value() < x.getValue()) x.getValue())maximum = x;maximum = x;count++;count++;}}public Coin getMaximum(){public Coin getMaximum(){return maximum;return maximum;}}private double sum;private double sum;private Coin maximum;private Coin maximum;private int count; private int count; } }InterfacesInterfacesThe mechanics of analyzing the data is The mechanics of analyzing the data is the same in all cases; details of the same in all cases; details of measurement differ measurement differ We have three classes doing three very We have three classes doing three very similar tasks, but they all contain similar tasks, but they all contain redundant redundant codecodeClasses could agree on a method Classes could agree on a method getMeasure()getMeasure() that obtains the measure to that obtains the measure to be used in the analysis be used in the analysisInterfacesInterfacesWe can then implement a single We can then implement a single reusable reusable DataSet DataSet class whose add class whose add method looks like this: method looks like this: sum = sum + x.getMeasure(); sum = sum + x.getMeasure(); if (count == 0 || maximum.getMeasure() < if (count == 0 || maximum.getMeasure() < x.getMeasure()) x.getMeasure()) {{maximum = x; count++; maximum = x; count++; }}InterfacesInterfacesWhat type is What type is xx? ? We want We want xx to be an type of object that to be an type of object that has a has a getMeasure()getMeasure() method methodInterfaces allow us to ensure that Interfaces allow us to ensure that this is the casethis is the caseInterfacesInterfacesAn An interface typeinterface type is used to specify is used to specify required operations for a classrequired operations for a classpublic interface Measurable public interface Measurable {{double getMeasure(); double getMeasure(); } }InterfacesInterfacesJava uses interfaces to define a common Java uses interfaces to define a common set of behaviors that varying objects can set of
View Full Document