Sunday, May 31, 2009

Puzzle 28 -Solution.

I got a lot of unexpected solutions for this puzzle. I say unexpected because it's really not possible to know all possible solutions to the questions I post. Really – there so much room for improvisation.

Most of the questions are based on a Java principle. There are a few hints as to what I am looking for – so let's see what this puzzle was all about.

The solution that I had in mind for this puzzle was to add the static modifier to both the base and derived class fooBar() method.
In case of static functions – static over ride would apply the base class function would get called irrespective of the object pointed by the reference.
Adding a static modifier to a function does not change its signature – a signature of a function consists of its name, return type and parameters. Everything else is a modifier for the function.

The other common solutions were
a. Making Base reference point to Base class object. (Now I do remember why I had originally kept the Base class abstract!)
b. Calling super.fooBar() from the Derived Classes foobar().

Truthfully the challenge was --> 'Java dictates that polymorphism come into play when the Base class reference points to the derived class Object. Lets change that shall we?'
This means you had to stop the polymorphic behavior of Java from coming into play.

Nonetheless I'll consider any solution that gets 'Base Ahoy!' printed instead of 'Derived Ahoy' as correct!!

No comments:

Post a Comment

Solution for this question?