Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Course c1 = new Course(false, 123, “CMP”, 4)
- Course c2 = new Course(false, 175, “MAT”, 3)
- c1.compareTo(c2);
- //would return -1 because c1.courseNum < c2.courseNum
- c2.compareTo(c1); // would return 1
- "An employee is a person"
- The Person class has the method compareTo(Person p)
- 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)
- Why? Because these are all types of Person objects via inheritance rules
- EXTENDS :-)
- You don’t need to, and you should NOT implement the Comparable Interface in the child classes
- public class Person implements Comparable<Person> {……
- 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
- 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???
- Answer: No
- So how do we address this issue???
- Do you remember how to check if instanceof Employee?
- Jansen - quick question about the employee class. For its equal method, is it always going to be false because every employee has different employee #?
- Teacher - YES even the employeeID should match for 2 Employee Objects to assure they are the same
- Justin - what do you return if the person isn’t an instanceof employee? super.compareTo?
- Teacher - YES! BINGO!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement