Re: Problem with Exercise 4, Interfaces, Thinking in Java



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
.



Relevant Pages

  • Re: Can static methods be virtual i.e. invoked polymorphically?
    ... then f1 in derived class is invoked. ... reference to an object." ... syntactically call a static method on an object reference, ... to be at compile time. ...
    (comp.lang.java.programmer)
  • Re: Can static methods be virtual i.e. invoked polymorphically?
    ... then f1 in derived class is invoked. ... Doesn't that depend on what the compile-time type of each respective ... reference to an object." ... syntactically call a static method on an object reference, ...
    (comp.lang.java.programmer)
  • RE: Inherited method
    ... the base Person class when accessing it with a reference of type PERSON ... public string Description ... want to get rid of the compiler warning you can use the new keyword i.e.: ... derived class then you need to make the method virtual in the base class and ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: attribute or subclass
    ... With inheritance the derived class code ... reference in place of the base class reference. ...
    (comp.object)
  • Re: Inheritance Woes
    ... class are compiled into separate assemblies. ... attempting to reference the derived class in a project and had assumed ... that the base class assembly would automatically be referenced via the ...
    (microsoft.public.dotnet.languages.csharp)