100 likes | 112 Views
Recitation 8. October 14, 2011. Today’s Goals:. Overriding versus overloading a method. Notes. Show off your work! Record what you have done using screen video capture software Upload to YouTube Send a link to comp401-help Doesn’t replace screenshots Not Graded.
E N D
Recitation 8 October 14, 2011
Today’s Goals: • Overriding versus overloading a method
Notes • Show off your work! • Record what you have done using screen video capture software • Upload to YouTube • Send a link to comp401-help • Doesn’t replace screenshots • Not Graded
Extending the Object class • All classes extend the Object class either directly or indirectly
Overloading • overloaded methods: • appear in the same class or a subclass • have the same name but, • have different parameter lists, and, • can have different return types [Java Quick Reference]
Overloading Object equals() public booleanequals(Example otherExample) { if (otherExample != null) { return (this.getX() == otherExample.getX() && this.getY() == otherExample.getY()); } else return false; } equals defined in Example & Object
Overriding • Overridden methods: • appear in subclasses • have the same name as a superclass method • have the same parameter list as a superclass method • have the same return type as a superclass method • A few other conditions you’ll learn later. [Java Quick Reference]
Overriding Object equals() public boolean equals(Object obj) { if(objinstanceof Example && obj != null) { Example otherObj = (Example)obj; return (this.getX() == otherObj.getX() && this.getY() == otherObj.getY()); } else return false; } equals defined in Example
Recitation Specification Add equals(…) to CartesianLine which overloads Object.equals() Add equals(…) to CartesianTriangle which overrides Object.equals() Bonus (for candy): Make equals work in a graphical sense.