Re: ClassCast Exception
ba.hons wrote:
Thanks for the help i will explain it slighlty more high level.
I have superclass called Wrapper that wraps up methods of a C++ dll
I need to create a new c++ dll with a couple of extra methods but i
cant add them to the original dll. So i thought the best approach would
be to create a new Wrapper class like so
public class NewWrapper extends Wrapper{
It's tricky to impose a relationship between extrinsic components.
This inheritance structure binds you to a similar inheritance of the external
entity "new DLL" from the "original DLL". You are committing your Java design
to mirror the C++ inheritance, only even minimally manageable if you control
the relationship between the DLLs. If the DLL inheritance relationship is
extrinsic to the Java project and not guaranteed to apply, then potentially
you have introduced brittleness.
This does not mean literally "inheritance" by one DLL's components of
another's. I don't know of a meaningful way to say that one DLL "inherits
from" another. It is sufficiently safe for your Java inheritance model if the
interface contracts for the DLLs will always fit the equivalent logical
inheritance model between them.
Does the new DLL really need to duplicate methods from the original, instead
of merely adding new methods and calling the original as appropriate? Do you
control the specifications of the original DLL? What if it comes out in a new
version?
An alternative approach is for NewWrapper to wrap Wrapper rather than to
inherit it. This insulates the Java code a mite from changes to the extrinsic
components, at a cost of some initial effort. Keep asking, "Is it 'is-a' or
'has-a' here?"
- Lew