Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 COS 260 DAY 19 Tony Gauvin. 2 Agenda Questions? 8 th Mini Quiz not corrected yet 9 Th Mini Quiz next class –Due November 19 Finish Discussion on More.

Similar presentations


Presentation on theme: "1 COS 260 DAY 19 Tony Gauvin. 2 Agenda Questions? 8 th Mini Quiz not corrected yet 9 Th Mini Quiz next class –Due November 19 Finish Discussion on More."— Presentation transcript:

1 1 COS 260 DAY 19 Tony Gauvin

2 2 Agenda Questions? 8 th Mini Quiz not corrected yet 9 Th Mini Quiz next class –Due November 19 Finish Discussion on More on Inheritance

3 3 Final Countdown Nov 16 –Finish Chap 9 Nov 19 –9 th Mini Quiz –Assignment 5 due –Begin Chap 10 Nov 23 –Finish Chapter 10 Nov 30 –10 th Mini quiz –Begin Chap 11 Dec 3 –Finish Chapter 11 –Assignment 6 due Dec 7 –11 th mini quiz –Begin Chapter 12 Dec 10 –Finish Chapter 12 –Assignment 7 Due Dec 14 @ 10 AM –12 th mini quiz –Capstone Presentations Copyright © 2014 Pearson Education, Inc. Slide 1-3

4 4 Change to Grade Calculations 12 instead of 11 mini quizzes –Still best 10 @ 3% each 7 instead of 8 assignments –AVG(7) = 40% of final grade –Was 8 @ 5% each Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

5 More about inheritance Exploring polymorphism 5.0

6 6 Question What is the difference between static type and dynamic type in JAVA variable assignments? What is method polymorphism> Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

7 7 The instanceof operator Used to determine the dynamic type. Recovers ‘lost’ type information. Usually precedes assignment with a cast to the dynamic type: if(post instanceof MessagePost) { MessagePost msg = (MessagePost) post; … access MessagePost methods via msg … } Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

8 8 The Object class’s methods Methods in Object are inherited by all classes. Any of these may be overridden. The toString method is commonly overridden: –public String toString() –Returns a string representation of the object. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

9 9 Overriding toString in Post Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling public String toString() { String text = username + "\n" + timeString(timestamp); if(likes > 0) { text += " - " + likes + " people like this.\n"; } else { text += "\n"; } if(comments.isEmpty()) { return text + " No comments.\n"; } else { return text + " " + comments.size() + " comment(s). Click here to view.\n"; }

10 10 Overriding toString Explicit print methods can often be omitted from a class: System.out.println(post.toString()); Calls to println with just an object automatically result in toString being called: System.out.println(post); Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

11 11 StringBuilder Consider using StringBuilder as an alternative to concatenation: StringBuilder builder = new StringBuilder(); builder.append(username); builder.append('\n'); builder.append(timeString(timestamp)); … return builder.toString(); Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

12 12 Object equality What does it mean for two objects to be ‘the same’? –Reference equality. –Content equality. Compare the use of == with equals() between strings. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

13 13 Overriding equals Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling public boolean equals(Object obj) { if(this == obj) { return true; } if(!(obj instanceof ThisType)) { return false; } ThisType other = (ThisType) obj; … compare fields of this and other }

14 14 Overriding equals in Student Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling public boolean equals(Object obj) { if(this == obj) { return true; } if(!(obj instanceof Student)) { return false; } Student other = (Student) obj; return name.equals(other.name) && id.equals(other.id) && credits == other.credits; }

15 15 Overriding hashCode in Student Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling /** * Hashcode technique taken from * Effective Java by Joshua Bloch. */ public int hashCode() { int result = 17; result = 37 * result + name.hashCode(); result = 37 * result + id.hashCode(); result = 37 * result + credits; return result; }

16 16 Protected access Private access in the superclass may be too restrictive for a subclass. The closer inheritance relationship is supported by protected access. Protected access is more restricted than public access. We still recommend keeping fields private. –Define protected accessors and mutators. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

17 17 Access levels Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

18 18 Review The declared type of a variable is its static type. –Compilers check static types. The type of an object is its dynamic type. –Dynamic types are used at runtime. Methods may be overridden in a subclass. Method lookup starts with the dynamic type. Protected access supports inheritance. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling


Download ppt "1 COS 260 DAY 19 Tony Gauvin. 2 Agenda Questions? 8 th Mini Quiz not corrected yet 9 Th Mini Quiz next class –Due November 19 Finish Discussion on More."

Similar presentations


Ads by Google