DSA Factory
Free Object-oriented basics lessonsObject-oriented basics · Stage 2 · Inheritance and polymorphism · Step 2

Overriding and overloading

Two ways the same method name appears twice, decided at different times, and the rules that tell them apart. About 13 minutes.

Quick check

A class defines equals(Point p) intending to override Object's equals. Why might a HashSet<Point> ignore it?

  1. AIt overloads equals rather than overriding it, since the parameter type is Point, not Object
  2. BBecause equals is static
  3. CBecause Point has no hashCode
Show the answer

It overloads equals rather than overriding it, since the parameter type is Point, not Object. Collections call equals(Object); the overload is never used. @Override would have caught it.

Lock it in

4 questions on this idea

Short questions that apply the idea to new cases: trace it, work out a number, put steps in order. A wrong answer explains itself.

Answer them in your browserFree account, no card. Your progress is saved, and the idea comes back in a quick review later.
Sign up free to solve