returning a subclass through an interface

  • Thread starter Thread starter Andy Fish
  • Start date Start date
A

Andy Fish

Hi,

I am implementing an API call which returns an object of type X. In my code,
I build an object which is of type Y - a subclass of X.

I'm worried that if I return the object Y, the caller will be able to use
reflection or a debugger to find out about the extra properties of Y. Is
this concern valid or is there something inherent that stops him doing this?

assuming my concern is valid, I need to make a "clone" of Y but for the
clone to be a superclass (i.e. X). Is there any clever way of doing this?
The class X only contains public immutable properties and has no methods so
there is no need to deep copy.

TIA

Andy
 
Andy Fish said:
Hi,

I am implementing an API call which returns an object of type X. In my code,
I build an object which is of type Y - a subclass of X.

I'm worried that if I return the object Y, the caller will be able to use
reflection or a debugger to find out about the extra properties of Y. Is
this concern valid or is there something inherent that stops him doing this?

no, but if you don't want the caller to see Y, you shouldn't return Y in the
first place.
 
Back
Top