DOC PREVIEW
UNF COP 2551 - Lecture Notes

This preview shows page 1-2-19-20 out of 20 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 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 20 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 20 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 20 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Chapter 6The this ReferenceThe this referenceOutlineInterfacesSlide 6Note:Slide 8Slide 9Slide 10Slide 11Slide 12Slide 13Slide 14Slide 15The Comparable InterfaceImplementing the Comparable InterfaceThe Iterator InterfaceSlide 19Slide 20Chapter 6Object-Oriented DesignPart 2© 2004 Pearson Addison-Wesley. All rights reserved 2/20The this Reference•The this reference allows an object to refer to itself That is, the this reference, used inside a method, refers to the object through which the method is being executedRemember: instance variables are unique to each object; but the methods are shared!!•Suppose the this reference is used in a method called tryMe, which is invoked as follows:obj1.tryMe();obj2.tryMe();•In the first invocation, the this reference refers to obj1; in the second it refers to obj2© 2004 Pearson Addison-Wesley. All rights reserved 3/20The this reference•The this reference can be used to distinguish the instance variables of a class from corresponding method parameters with the same names•The constructor of the Account class (from Chapter 4) could have been written as follows:public Account (Sring name, long acctNumber, double balance){ this.name = name; this.acctNumber = acctNumber; this.balance = balance;}Here this.name refers to the instance variable for ‘this’ object… (don’t need different formal parameter names)© 2004 Pearson Addison-Wesley. All rights reserved 4/20OutlineSoftware Development ActivitiesIdentifying Classes and ObjectsStatic Variables and MethodsClass RelationshipsInterfacesEnumerated Types RevisitedMethod DesignTestingGUI Design and Layout© 2004 Pearson Addison-Wesley. All rights reserved 5/20Interfaces•A Java interface is a collection of abstract methods and constants•An abstract method is a method header without a method body•An abstract method can be declared using the modifier abstract, but because all methods in an interface are abstract, usually it is left off•An interface is used to establish a set of methods that a class will implement.•Note: the Java interface is itself NOT a class!© 2004 Pearson Addison-Wesley. All rights reserved 6/20Interfacespublic interface Doable{ public void doThis(); public int doThat(); public void doThis2 (float value, char ch); public boolean doTheOther (int num);}interface is a reserved word(note: ‘class’ is not used)None of the methods inan interface are givena definition (body)A semicolon immediatelyfollows each method headerThis ‘prevents’ there being a body for the method.These are all abstract methods because they are declared within an ‘interface.’ They could be preceded with ‘abstract’ but this is not necessary!© 2004 Pearson Addison-Wesley. All rights reserved 7/20Note:•In defining ‘classes,’ we say: public class whatever•In defining an ‘interface,’ we say public interface whatever (we do Not say ‘class.’)© 2004 Pearson Addison-Wesley. All rights reserved 8/20Interfaces•An interface cannot be instantiated.Recall, it is NOT a class! (thus cannot be instantiated!)•Methods in an interface have public visibility by default A class formally implements an interface by:stating so in the class headerproviding implementations for each abstract method in the interface•If a class asserts that it “implements an interface,” it must define all methods in the interface© 2004 Pearson Addison-Wesley. All rights reserved 9/20Interfacespublic class CanDo implements Doable{ public void doThis () { // whatever } public void doThat () { // whatever } // etc.}implements is areserved wordEach method listedin Doable isgiven a definitionNote: this class ‘implements’ the interface. All abstract methods in the interface must be defined here in the class that ‘implements’ the interface.If all the methods are not defined, then this class is itself abstract and cannotbe instantiated.© 2004 Pearson Addison-Wesley. All rights reserved 10/20Interfaces•A class that implements an interface can implement other methods as well•See Complexity.java (ahead)•See Question.java (ahead)•See MiniQuiz.java (ahead)•In addition to (or instead of) abstract methods, an interface can contain constants•When a class implements an interface, it gains access to all its constants© 2004 Pearson Addison-Wesley. All rights reserved 11/20//************************************************// Complexity.java Author: Lewis/Loftus//// Represents the interface for an object that can be assigned // an explicit complexity.//************************************************public interface Complexity{ public void setComplexity (int complexity); public int getComplexity();}Note the semicolon after the abstract methods!Note: public  interface…This interface allows many classes to ‘implement’ it in different ways.© 2004 Pearson Addison-Wesley. All rights reserved 12/20// Question.java Author: Lewis/Loftus // Represents a question (and its answer).public class Question implements Complexity{ private String question, answer; private int complexityLevel; // Constructor: Sets up the question with a default complexity. public Question (String query, String result) // Of course, we usually need a Constructor…. { question = query; answer = result; complexityLevel = 1; } // Sets the complexity level for this question. public void setComplexity (int level) // Must have this one { complexityLevel = level; } // Returns the complexity level for this question. public int getComplexity() // Must have this one. { return complexityLevel; } // Returns the question. public String getQuestion() { return question; } // Do NOT code like I have the next two methods. Only for space here! // Returns the answer to this question. public String getAnswer() { return answer; } // Returns true if the candidate answer matches the answer. public boolean answerCorrect (String candidateAnswer) { return answer.equals(candidateAnswer); } // Returns this question (and its answer) as a string. public String toString() { return question + "\n" + answer; }© 2004 Pearson Addison-Wesley. All rights reserved 13/20// MiniQuiz.java Author: Lewis/Loftus// Demonstrates the use


View Full Document

UNF COP 2551 - Lecture Notes

Download Lecture Notes
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 Lecture Notes 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 Lecture Notes 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?