Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. Course c1 = new Course(false, 123, “CMP”, 4)
  2. Course c2 = new Course(false, 175, “MAT”, 3)
  3. c1.compareTo(c2);
  4. //would return -1 because c1.courseNum < c2.courseNum
  5. c2.compareTo(c1); // would return 1
  6.  
  7. "An employee is a person"
  8.  
  9. The Person class has the method compareTo(Person p)
  10. In all classes that inherit from the Person class, for example: Employee, Faculty, Student, and any other… their compareTo methods will also have the same signature … compareTo(Person p)
  11. Why? Because these are all types of Person objects via inheritance rules
  12. EXTENDS :-)
  13.  
  14. You don’t need to, and you should NOT implement the Comparable Interface in the child classes
  15. public class Person implements Comparable<Person> {……
  16. that’s the only place the interface should be implemented… not in any of the child classes… but the method should be overridden whenever specified
  17. Here is the caveat…. Is every Person that could potentially be passed into the compareTo(Person p) of a child class … Employee for example.. an Employee? an instanceof the class we’re in???
  18. Answer: No
  19. So how do we address this issue???
  20. Do you remember how to check if instanceof Employee?
  21.  
  22. Jansen - quick question about the employee class. For its equal method, is it always going to be false because every employee has different employee #?
  23. Teacher - YES even the employeeID should match for 2 Employee Objects to assure they are the same
  24.  
  25. Justin - what do you return if the person isn’t an instanceof employee? super.compareTo?
  26. Teacher - YES! BINGO!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement