comp 110 some notes on inheritance, review luv kohli december 1, 2008 mwf 2-2:50 pm sitterson 014
DESCRIPTION
Questions? 3TRANSCRIPT
![Page 1: COMP 110 Some notes on inheritance, review Luv Kohli December 1, 2008 MWF 2-2:50 pm Sitterson 014](https://reader034.vdocuments.net/reader034/viewer/2022052609/5a4d1bae7f8b9ab0599cbadd/html5/thumbnails/1.jpg)
COMP 110COMP 110Some notes on inheritance, reviewSome notes on inheritance, review
Luv KohliDecember 1, 2008
MWF 2-2:50 pmSitterson 014
![Page 2: COMP 110 Some notes on inheritance, review Luv Kohli December 1, 2008 MWF 2-2:50 pm Sitterson 014](https://reader034.vdocuments.net/reader034/viewer/2022052609/5a4d1bae7f8b9ab0599cbadd/html5/thumbnails/2.jpg)
AnnouncementsAnnouncementsFinal exam, comprehensive◦Saturday, December 6, 4pm
2
![Page 3: COMP 110 Some notes on inheritance, review Luv Kohli December 1, 2008 MWF 2-2:50 pm Sitterson 014](https://reader034.vdocuments.net/reader034/viewer/2022052609/5a4d1bae7f8b9ab0599cbadd/html5/thumbnails/3.jpg)
Questions?Questions?
3
![Page 4: COMP 110 Some notes on inheritance, review Luv Kohli December 1, 2008 MWF 2-2:50 pm Sitterson 014](https://reader034.vdocuments.net/reader034/viewer/2022052609/5a4d1bae7f8b9ab0599cbadd/html5/thumbnails/4.jpg)
Final examFinal examFinal exam will cover everything we have
covered◦Lectures◦Readings from textbook◦Labs◦Programs◦ In-class exercises/worksheets◦Midterm
4
![Page 5: COMP 110 Some notes on inheritance, review Luv Kohli December 1, 2008 MWF 2-2:50 pm Sitterson 014](https://reader034.vdocuments.net/reader034/viewer/2022052609/5a4d1bae7f8b9ab0599cbadd/html5/thumbnails/5.jpg)
Final exam review on WednesdayFinal exam review on WednesdayNo formal reviewLook over previous assignments, lectures,
midterm, etc.Come prepared with questions
5
![Page 6: COMP 110 Some notes on inheritance, review Luv Kohli December 1, 2008 MWF 2-2:50 pm Sitterson 014](https://reader034.vdocuments.net/reader034/viewer/2022052609/5a4d1bae7f8b9ab0599cbadd/html5/thumbnails/6.jpg)
Today in COMP 110Today in COMP 110Revisiting the equals method
Array and inheritance review
6
![Page 7: COMP 110 Some notes on inheritance, review Luv Kohli December 1, 2008 MWF 2-2:50 pm Sitterson 014](https://reader034.vdocuments.net/reader034/viewer/2022052609/5a4d1bae7f8b9ab0599cbadd/html5/thumbnails/7.jpg)
The The instanceofinstanceof operator operatorWe 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
![Page 8: COMP 110 Some notes on inheritance, review Luv Kohli December 1, 2008 MWF 2-2:50 pm Sitterson 014](https://reader034.vdocuments.net/reader034/viewer/2022052609/5a4d1bae7f8b9ab0599cbadd/html5/thumbnails/8.jpg)
The The equalsequals method methodThird 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
![Page 9: COMP 110 Some notes on inheritance, review Luv Kohli December 1, 2008 MWF 2-2:50 pm Sitterson 014](https://reader034.vdocuments.net/reader034/viewer/2022052609/5a4d1bae7f8b9ab0599cbadd/html5/thumbnails/9.jpg)
The The equalsequals method method Implements an equivalence relation:
It is reflexive◦ For any non-null reference value x, x.equals(x) should return
true
9
![Page 10: COMP 110 Some notes on inheritance, review Luv Kohli December 1, 2008 MWF 2-2:50 pm Sitterson 014](https://reader034.vdocuments.net/reader034/viewer/2022052609/5a4d1bae7f8b9ab0599cbadd/html5/thumbnails/10.jpg)
The The equalsequals method 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
![Page 11: COMP 110 Some notes on inheritance, review Luv Kohli December 1, 2008 MWF 2-2:50 pm Sitterson 014](https://reader034.vdocuments.net/reader034/viewer/2022052609/5a4d1bae7f8b9ab0599cbadd/html5/thumbnails/11.jpg)
The The equalsequals method 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
![Page 12: COMP 110 Some notes on inheritance, review Luv Kohli December 1, 2008 MWF 2-2:50 pm Sitterson 014](https://reader034.vdocuments.net/reader034/viewer/2022052609/5a4d1bae7f8b9ab0599cbadd/html5/thumbnails/12.jpg)
The The equalsequals method 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
![Page 13: COMP 110 Some notes on inheritance, review Luv Kohli December 1, 2008 MWF 2-2:50 pm Sitterson 014](https://reader034.vdocuments.net/reader034/viewer/2022052609/5a4d1bae7f8b9ab0599cbadd/html5/thumbnails/13.jpg)
The The equalsequals method methodFor any non-null reference value x, x.equals(null) should
return false
13
![Page 14: COMP 110 Some notes on inheritance, review Luv Kohli December 1, 2008 MWF 2-2:50 pm Sitterson 014](https://reader034.vdocuments.net/reader034/viewer/2022052609/5a4d1bae7f8b9ab0599cbadd/html5/thumbnails/14.jpg)
The The equalsequals method methodThis 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
![Page 15: COMP 110 Some notes on inheritance, review Luv Kohli December 1, 2008 MWF 2-2:50 pm Sitterson 014](https://reader034.vdocuments.net/reader034/viewer/2022052609/5a4d1bae7f8b9ab0599cbadd/html5/thumbnails/15.jpg)
The The equalsequals method methodWhy?◦ 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
![Page 16: COMP 110 Some notes on inheritance, review Luv Kohli December 1, 2008 MWF 2-2:50 pm Sitterson 014](https://reader034.vdocuments.net/reader034/viewer/2022052609/5a4d1bae7f8b9ab0599cbadd/html5/thumbnails/16.jpg)
The The equalsequals method 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
![Page 17: COMP 110 Some notes on inheritance, review Luv Kohli December 1, 2008 MWF 2-2:50 pm Sitterson 014](https://reader034.vdocuments.net/reader034/viewer/2022052609/5a4d1bae7f8b9ab0599cbadd/html5/thumbnails/17.jpg)
COMP110 Battle … of DoomCOMP110 Battle … of Doom
17
Lazy LennyWeapon/Special ability:
sleep
MagicalFlying amphibianWeapon/Special ability:
fly, make smoke, disappearVS
WINNER
![Page 18: COMP 110 Some notes on inheritance, review Luv Kohli December 1, 2008 MWF 2-2:50 pm Sitterson 014](https://reader034.vdocuments.net/reader034/viewer/2022052609/5a4d1bae7f8b9ab0599cbadd/html5/thumbnails/18.jpg)
COMP110 Battle … of DoomCOMP110 Battle … of Doom
18
SpringWeapon/Special ability:
allergies
MagicalFlying amphibianWeapon/Special ability:
fly, make smoke, disappearVS
WINNER
It’s tough to fly when you are suffering from
allergies!
![Page 19: COMP 110 Some notes on inheritance, review Luv Kohli December 1, 2008 MWF 2-2:50 pm Sitterson 014](https://reader034.vdocuments.net/reader034/viewer/2022052609/5a4d1bae7f8b9ab0599cbadd/html5/thumbnails/19.jpg)
COMP110 Battle … of DoomCOMP110 Battle … of Doom
19
SpringWeapon/Special ability:
allergies
StarburstManWeapon/Special ability:
http://seattlest.com/2008/09/23/local_man_assaulted_by_starburst_ca.php
VS
WINNER
![Page 20: COMP 110 Some notes on inheritance, review Luv Kohli December 1, 2008 MWF 2-2:50 pm Sitterson 014](https://reader034.vdocuments.net/reader034/viewer/2022052609/5a4d1bae7f8b9ab0599cbadd/html5/thumbnails/20.jpg)
Array and Inheritance review worksheetArray and Inheritance review worksheet
20
![Page 21: COMP 110 Some notes on inheritance, review Luv Kohli December 1, 2008 MWF 2-2:50 pm Sitterson 014](https://reader034.vdocuments.net/reader034/viewer/2022052609/5a4d1bae7f8b9ab0599cbadd/html5/thumbnails/21.jpg)
WednesdayWednesdayFinal exam review
Come prepared with questions!
21