Sponsored Link •
This page contains an archived post to the Design Forum (formerly called the Flexible Java Forum) made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.
> > Here's a little problem I've been trying to solve.
> > I have an abstract base class with two concrete subclasses.
> > abstract class Animal ...
> > Right, I want to write a method which does something with
> > void buy(Cat c) ...
> > So far so good. But in my client code, I store references
> > Animal animal = new Dog();
> > Now, when I try to call: buy(animal);
> > So, my questions are:
> > Any help greatly appreciated.
> > Cheers,
> > Chris
> What u are trying to imeplement in you code is dynamic polymorphism and you are using overloading which is wrong.
I chanced upon this message searching the web for something else, and my god, poor Chris.
Yes the answer is in the reply, but talk about obfuscating the answer. In the example, Chris isn't doing any overloading, he's just missing an abstract method in the abstract parent. He has failed to define the abstract behaviour buy( Animal) that every Animal implementation must provide. (or throw UnsupportedOperationException( "buy is not supported for Birds") if appropriate).
I'm sure Chris figured this out from the reply, but the reply itself is such a shocker, I had to write this.