Re: Problem with Exercise 4, Interfaces, Thinking in Java
- From: "Peter Duniho" <NpOeStPeAdM@xxxxxxxxxxxxxxxx>
- Date: Mon, 28 Jan 2008 22:50:51 -0800
On Mon, 28 Jan 2008 22:35:55 -0800, Dural <dwc4@xxxxxxxxxxx> wrote:
"Create an abstract class with no methods. Derive a class and add a
method. Create a static method that takes a reference to the base
class, downcast it to the derived class, and calls the method. In
main(),demonstrate that it works. Now put the abstract declaration
for the method in the base class, thus eliminating the need for the
downcast."
Here's my interpretation. But of course I can't create an object
that's abstract, so running this code fails. Not to mention that you
can't downcast an object that was never a derived class to begin with!
Am I misreading what I'm supposed to be doing here?
The main thing is that obviously to downcast successfully, you need to start with an instance of the derived class. I also interpret "takes a reference" to mean that the method should take as a parameter that reference.
With that in mind, here's an edited version of the original instructions (my additions in "[]"):
"Create an abstract class with no methods. Derive a class and add a
method. Create a static method [in a different class, e.g. Ex4] that
takes [as a parameter] a reference to the base class, downcast it to
the derived class, and calls the method. In main(), demonstrate that
it works [by creating an instance of the derived class and passing it
to the static method]. Now put the abstract declaration for the method
in the base class, thus eliminating the need for the downcast."
The first modification I made just because I think it's clearer if the static method isn't participating in the definition of the interesting classes. The second and third are clarifications that I think are needed in order to make the exercise make more sense.
If those are the only instructions you've been given, I have to say they seem a bit vague. But then, that's nothing new. You just have to be able to discard interpretations that just don't make sense, and figure out which ones do (and hopefully once you've done that, you're left with only one :) ).
Pete
.
- Follow-Ups:
- References:
- Prev by Date: Do You Want a GSM Mobile with Amazing Features? Please click here
- Next by Date: Re: Problem with Exercise 4, Interfaces, Thinking in Java
- Previous by thread: Problem with Exercise 4, Interfaces, Thinking in Java
- Next by thread: Re: Problem with Exercise 4, Interfaces, Thinking in Java
- Index(es):
Relevant Pages
|