Multiple choice

Assume that File is an abstract class and has toFile() method. ImageFile and BinaryFile are concrete classes of the abstract class File. Also, assume that the method toFile() is implemented in both Binary File and Image File. A File references an ImageFile object in memory and the toFile method is called, which implementation method will be called?

  1. Binary File

  2. Image File

  3. Both File and Binary Files

  4. None of the above

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

This is polymorphism through dynamic method dispatch. When a superclass reference points to a subclass object (File reference to ImageFile), and a method is called, Java uses the actual object's type (not the reference type) to determine which implementation to execute. Since the reference points to an ImageFile object, the ImageFile's toFile() method will be called.