Download presentation
Presentation is loading. Please wait.
Published byJade Dixon Modified over 9 years ago
1
COMP 110 Some notes on inheritance, review Luv Kohli December 1, 2008 MWF 2-2:50 pm Sitterson 014
2
Announcements Final exam, comprehensive ◦ Saturday, December 6, 4pm 2
3
Questions? 3
4
Final exam Final exam will cover everything we have covered ◦ Lectures ◦ Readings from textbook ◦ Labs ◦ Programs ◦ In-class exercises/worksheets ◦ Midterm 4
5
Final exam review on Wednesday No formal review Look over previous assignments, lectures, midterm, etc. Come prepared with questions 5
6
Today in COMP 110 Revisiting the equals method Array and inheritance review 6
7
The instanceof operator We can test whether an object is of a certain class type: if (obj instanceof Student) { System.out.println("obj is an instance of the class Student"); } Syntax: object instanceof Class_Name Use this operator in the equals method 7
8
The equals method Third try public boolean equals(Object obj) { if ((obj != null) && (obj instanceof Student)) { Student otherStudent = (Student) obj; return (this.id == otherStudent.id); } return false; } Reminder: null is a special constant that can be assigned to a variable of a class type – means that the variable does not refer to anything right now 8
9
The equals method Implements an equivalence relation: It is reflexive ◦ For any non-null reference value x, x.equals(x) should return true 9
10
The equals method It is symmetric ◦ For any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true 10
11
The equals method It is transitive ◦ For any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true 11
12
The equals method It is consistent ◦ For any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified 12
13
The equals method For any non-null reference value x, x.equals(null) should return false 13
14
The equals method This implementation is not symmetric public boolean equals(Object obj) { if ((obj != null) && (obj instanceof Student)) { Student otherStudent = (Student) obj; return (this.id == otherStudent.id); } return false; } Demonstrate in jGRASP 14
15
The equals method Why? ◦ The instanceof operator will return true if obj is a subclass of Student public boolean equals(Object obj) { if ((obj != null) && (obj instanceof Student)) { Student otherStudent = (Student) obj; return (this.id == otherStudent.id); } return false; } Demonstrate problem in jGRASP 15
16
The equals method Fourth try public boolean equals(Object obj) { if (this == obj) return true; if ((obj == null) || (obj.getClass() != this.getClass())) return false; Student otherStudent = (Student) obj; return (this.id == otherStudent.id); } The getClass method will return the runtime class of an object, so if obj is a subclass of Student (in this case), this method will return false 16
17
COMP110 Battle … of Doom 17 Lazy Lenny Weapon/Special ability: sleep MagicalFlying amphibian Weapon/Special ability: fly, make smoke, disappear VS WINNER
18
COMP110 Battle … of Doom 18 Spring Weapon/Special ability: allergies MagicalFlying amphibian Weapon/Special ability: fly, make smoke, disappear VS WINNER It’s tough to fly when you are suffering from allergies!
19
COMP110 Battle … of Doom 19 Spring Weapon/Special ability: allergies StarburstMan Weapon/Special ability: http://seattlest.com/2008/09/23/local_man _assaulted_by_starburst_ca.php http://seattlest.com/2008/09/23/local_man _assaulted_by_starburst_ca.php VS WINNER
20
Array and Inheritance review worksheet 20
21
Wednesday Final exam review Come prepared with questions! 21
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.