A
Aaron Fude
Hi,
This seems contrary to Java's design, but I will ask the question
anyway.
Suppose I want to have
public interface Element {
public Element copy();
}
and
public class MyClass implements Element {
public MyClass copy() {
return new MyClass();
}
}
This is invalid Java but can something like this be accomplished with
Java 5 features?
Here is another question of the same kind. Suppose I am implementing
some kind of mathematical operator theory package. I have to concepts:
LinearOperator and Sum with the property that LO(a+b) = LO(a) + LO(b)
I believe this logic belongs at the LinearOperator level, rather than
at the level of a derived class, such as (TimeDerivative extends
LinearOperator).
So here's the type of method I'd like to write in LinearOperator (fake
code):
public class LinearOperator {
Element argument;
public Sum distribute() {
if (argument.isASum()) {
Sum sum = (Sum) argument;
return new Sum(new LinearOperator(sum.a), new LinearOperator
(sum.b));
}
throw new RuntimeException();
}
}
However, rather than creating an instance of LinearOperator, I need to
create an instance of the derived class, such as TimeDerivative.
So is there a good way to keep this logic at the LinearOperator level?
This seems contrary to Java's design, but I will ask the question
anyway.
Suppose I want to have
public interface Element {
public Element copy();
}
and
public class MyClass implements Element {
public MyClass copy() {
return new MyClass();
}
}
This is invalid Java but can something like this be accomplished with
Java 5 features?
Here is another question of the same kind. Suppose I am implementing
some kind of mathematical operator theory package. I have to concepts:
LinearOperator and Sum with the property that LO(a+b) = LO(a) + LO(b)
I believe this logic belongs at the LinearOperator level, rather than
at the level of a derived class, such as (TimeDerivative extends
LinearOperator).
So here's the type of method I'd like to write in LinearOperator (fake
code):
public class LinearOperator {
Element argument;
public Sum distribute() {
if (argument.isASum()) {
Sum sum = (Sum) argument;
return new Sum(new LinearOperator(sum.a), new LinearOperator
(sum.b));
}
throw new RuntimeException();
}
}
However, rather than creating an instance of LinearOperator, I need to
create an instance of the derived class, such as TimeDerivative.
So is there a good way to keep this logic at the LinearOperator level?