Common class methods Flashcards

1
Q

Describe what toString method is and why it should almost always be overridden

A
  • toString method provided by Object class just gives the class name along with hexadecimal representation of hashcode, which is not useful.
  • Classes should override this method and provide useful information about the object so that when it’s sent to println, assert, or in string, user have useful information.
  • If possible, provide all information about the object. If that’s too large, provide a summary.
  • You can elect to document the form of string toString will print out. In this case, you can’t change this format and the you should provide a constructor that takes the string as input. Alternatively, you can just not specify the form.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Describe equals method and properties that must be obeyed by objects overriding equals method

A
  • equals method should be overriden for classes containing values so that classes with equal values makes equals method returns true.
  • When overriding equals method, make sure that the method is
    1) Reflexive. x.equals(x) must always be true.
    2) Symmetric. x.equals(y) must mean y.equals(x). This is often a problem when we have a value class like Point with subclass like ColorPoint. Point.equals(ColorPoint) may return true while ColorPoint.equals(Point) may return false if ColorPoint only check for instanceof ColorPoint. You may think of ColorPoint checking for Point as well, but there’s problem with that below.
    3) Transitive. x.equals(y) and y.equals(z) means x.equals(z). If we have ColorPoint check for Point in above, ColorPointA.equals(PointB) is true and PointB.equals(ColorPointC) is true, but ColorPointA.equals(ColorPointC) may not be true.
  • The way around this is to avoid extending value classes and just create a wrapper with view method to get original class instead.
  • Be sure to override hashcode when overriding equals.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Describe Clonable interface, why it should be avoided, and alternatives.

A
  • Clonable is a bit of a weird interface. It doesn’t have any method. Classes that implements Clonable allow Object.clone() to return an instance of the class instead of throwing an exception.
  • clone() method is not supposed to call the constructor, but call super.clone() all the way up to Object.clone() where construction happen.
  • You have to fix references for resulting object everywhere appropriate, which means some fields cannot be made final.
  • If your superclass does not call super.clone(), you are out of luck.
  • Avoid doing this when possible and provide a copy constructor or copy static factory method instead.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Describe Comparable interface, when to use it, and how to implement it.

A
  • When a class you create have some notion of natural ordering, you should consider implementing Comparable. This allows your class to work with a bunch of library algorithms and sort properly.
  • Contract for Comparable is similar to equals - reflexive, symmetric, and transitive.
  • To make comparable work well, it’s a good idea that objects that test equal returns 0 when compared with compareTo, although this is not a requirement. This is because some data structure like TreeMap uses compareTo, while others like HashMap use equals.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly