B
Bernd
Hi,
I am having a problem to design
I got an abstract base class Base and
a couple of concrete subclasses, say
Child1 and Child2. With every class that is derived from
Base certain "cost" is associated
(a value, that despends only on the subclass, not on a
speific object of that class).
Now I would like to do the following:
1) Ask a subclass about its costs:
Child1.getCosts() // would return 10, because class Child1 costs 10
Child1.getCosts(); // would return 20, because class Child2 costs 20
2) ask a specific object about the costs of its subclass:
Child1 c1 = new Child1();
c1.getCosts() // would return 10, because class Child1 costs 10
Child1 c2 = new Child2();
c2.getCosts(); // would return 20, because class Child2 costs 20
3)ask a specific object about the costs of its subclass, but the reference
to the object is of class Base:
Base b;
b = new Child1();
b.getCosts() // would return 10, because class Child1 costs 10
b = new Child2();
b.getCosts(); // would return 20, because class Child2 costs 20
How do I design this?
(doesn't have to be a method getCosts(), could be via constants, etc)
I can't specify an abstract static method getCosts() in Base,
because Java does not allow that.
But if getCosts() is not static, I can't do Child1.getCosts().
If I leave it out of the base class, I always have to downcast every
Base object to its subclass, which I don't want, and there is
also no way to enforce the existence of a static getCosts function in
the subclasses.
I hope I made myself reasonable clear.
Please advice and thanks for you help.
Bernd
I am having a problem to design
I got an abstract base class Base and
a couple of concrete subclasses, say
Child1 and Child2. With every class that is derived from
Base certain "cost" is associated
(a value, that despends only on the subclass, not on a
speific object of that class).
Now I would like to do the following:
1) Ask a subclass about its costs:
Child1.getCosts() // would return 10, because class Child1 costs 10
Child1.getCosts(); // would return 20, because class Child2 costs 20
2) ask a specific object about the costs of its subclass:
Child1 c1 = new Child1();
c1.getCosts() // would return 10, because class Child1 costs 10
Child1 c2 = new Child2();
c2.getCosts(); // would return 20, because class Child2 costs 20
3)ask a specific object about the costs of its subclass, but the reference
to the object is of class Base:
Base b;
b = new Child1();
b.getCosts() // would return 10, because class Child1 costs 10
b = new Child2();
b.getCosts(); // would return 20, because class Child2 costs 20
How do I design this?
(doesn't have to be a method getCosts(), could be via constants, etc)
I can't specify an abstract static method getCosts() in Base,
because Java does not allow that.
But if getCosts() is not static, I can't do Child1.getCosts().
If I leave it out of the base class, I always have to downcast every
Base object to its subclass, which I don't want, and there is
also no way to enforce the existence of a static getCosts function in
the subclasses.
I hope I made myself reasonable clear.
Please advice and thanks for you help.
Bernd