Problem with Exercise 4, Interfaces, Thinking in Java

Discussion in 'Java' started by Dural, Jan 29, 2008.

  1. Dural

    Dural Guest

    "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?

    package interfaces;

    public class Ex4 {
    public static void main(String[] args) {
    Derived.g();
    }
    }

    abstract class Base { }

    class Derived extends Base {
    static void g() {
    Derived d = (Derived)(new Base()); // fails because I can't
    instantiate Base()
    d.f();
    }
    void f() {
    System.out.print("Derived.f()");
    }
    }
    Dural, Jan 29, 2008
    #1
    1. Advertising

  2. Dural

    Peter Duniho Guest

    On Mon, 28 Jan 2008 22:35:55 -0800, Dural <> 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
    Peter Duniho, Jan 29, 2008
    #2
    1. Advertising

  3. Dural

    Dural Guest

    Thanks for the feedback.. I think you are right, there was some
    implicit information in the wording, or maybe that was the whole
    trick :)

    package interfaces;

    public class Ex4 {
    public static void main(String[] args) {
    Derived d = new Derived();
    Ex4.g(d);
    }
    static void g(Base b) {
    Derived d = (Derived)(b);
    d.f();
    }

    }

    abstract class Base { }

    class Derived extends Base {
    void f() {
    System.out.print("Derived.f()");
    }
    Dural, Jan 29, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    8
    Views:
    1,833
  2. ge0rge
    Replies:
    5
    Views:
    518
  3. Progzmaster
    Replies:
    1
    Views:
    478
    Progzmaster
    Aug 23, 2006
  4. James Edward Gray II

    Thinking About Java Interfaces In Ruby

    James Edward Gray II, Nov 11, 2004, in forum: Ruby
    Replies:
    17
    Views:
    202
    Francis Hwang
    Nov 11, 2004
  5. optimistx

    Linear thinking vs essential thinking

    optimistx, Oct 28, 2009, in forum: Javascript
    Replies:
    4
    Views:
    175
    Dr J R Stockton
    Oct 29, 2009
Loading...

Share This Page