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