Problem with Exercise 4, Interfaces, Thinking in Java

D

Dural

"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()");
}
}
 
P

Peter Duniho

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

Dural

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()");
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top