returning a subclass through an interface

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
 
G

Guest

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top