DOC PREVIEW
UW-Madison COMPSCI 302 - Chapter 11 – Interfaces and Polymorphism

This preview shows page 1-2-16-17-18-33-34 out of 34 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 34 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 34 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 34 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 34 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 34 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 34 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 34 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 34 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

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 GoalsLearn about interfacesLearn about interfacesConvert between class and interface Convert between class and interface referencesreferencesUnderstand the concept of Understand the concept of polymorphismpolymorphismUnderstand the purpose of interfaces to Understand the purpose of interfaces to decouple classesdecouple classesInterfaces and Interfaces and PolymorphismPolymorphismInterfaces are important for Interfaces are important for developing reusable software developing reusable software componentscomponentsPolymorphism 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: InterfacesReal 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 classHow 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 ReuseUse Use interface typesinterface types to make code to make code more generalmore generalIdentify common/essential operationsIdentify common/essential operationsLet’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;} } } }ExampleExampleProblem: Only works for numbersProblem: Only works for numbersWhat 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; } }ExampleExampleWhat 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; } }InterfacesInterfacesThe 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 codecodeClasses 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 analysisInterfacesInterfacesWe 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++; }}InterfacesInterfacesWhat 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 methodInterfaces allow us to ensure that Interfaces allow us to ensure that this is the casethis is the caseInterfacesInterfacesAn 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(); } }InterfacesInterfacesJava 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

UW-Madison COMPSCI 302 - Chapter 11 – Interfaces and Polymorphism

Download Chapter 11 – Interfaces and Polymorphism
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Chapter 11 – Interfaces and Polymorphism and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Chapter 11 – Interfaces and Polymorphism 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?