Downcasting

Is it possible to “downcast” an instance of a superclass to a subclass?

For example:
I have class X that is a subclass of WebRequest.
I have an instance of a WebRequest named Request.

I’d like to do something like this:
Dim X1 As X = Request

Is that possible? I don’t think it’s that simple, but thought I’d ask.

Should be possible
I have renamed things to make it clearer in my mind which is the class, and which is the subclass (difficult terms, I think)

Class Dog is subclass of Mammal

[code]Dim Fido as Dog
Fido = new Dog (“Pug”)

Dim x as Mammal
if Fido IsA Mammal then
x = Mammal(d)
endif
[/code]

@Jeff Tullin: Thanks, but I think your example is upcasting a class - taking an instance of a subclass and using it to create an instance of the superclass.

What I’m trying to do is go the other way: Use an instance of a superclass to create an instance of a subclass. Using your example, I have an instance of Mammal, and I want to use it to create an instance of Dog.

create a class called “DOG” and set its superclass to “MAMMAL”
then add/override methods and properties that are unique to DOG

dim Corgi as Dog = Dog(Mammal)

I think?

@Dave S: Thanks, but creating a subclass isn’t the problem. I’m looking for a way downcast a superclass instance.

Using your example, I’m trying to take an instance of Mammal (the superclass) and use it to create an instance of Dog (the subclass).

Dim x as New Mammal Dim Fido As Dog Fido = x

I suspect that to do this, I’ll need to create a constructor on the subclass that takes the superclass as a parameter, and uses the properties of the superclass to explicitly set the subclass properties. I’m hoping that there’s an easier way.

subclasses are instances of the superclass (isa will confirm this)
instances of superclasses are not always instances of subclasses

ie/ all dogs are mammals (if dog ISA mammal)
not all mammals are dogs (if thisMammal ISA dog)

The only way to tell IF the cast to the subclass type would be legal is to do something like

If thisRequest isa Request then
     Dim X1 As Request = Request(thisRequest)
end if

Note that the framework won’t create instances of your subclass
You’d have to do that when they are received and the pass your subclass off to other handlers to deal with

Your subclass probably needs a constructor that takes an instance of the superclass and then this new instance of your subclass is created from that

@Tim Parnell - Yeah, I suspect you’re right. The issue is that I’d need to take the super class as a param, and in the subclass’s constructor, explicitly set each of the properties - which seems odd. Yet I can’t think of an easier way to do that.

Yup, I think that’s it, I was wrong in my original assumption.

Thanks, @Norman Palardy, and that’s what I suspected.

The approach that I’m considering - and it is an admittedly lazy way to do it - is to not subclass at all. Instead, I’ll treat the superclass as a property of the new class. So the new class won’t actually be a subclass, but it will give me an easy to accomplish the goal - which is to extend the superclass.

Continuing with the example above, here’s what it would look like:

Dim x as New Mammal Dim Fido As New Dog Fido.Mammal = x

Anyway, thanks everyone. This was an interesting issue, and I appreciate the feedback.

Dim x as New Mammal Dim Fido As New Dog Fido.Mammal = x

That’s still almost the ‘do it in the constructor’ approach, except that you are setting the internal object to point to an external object
The internal mammal object of a new Dog could therefore be null. Would that be valid?

constructor (x as mammal) _internalmammalobject = x end sub

Whereas if you instantiate a Mammal object in your Dog class, then a constructor passed a mammal can replicate what is passed instead of pointing at it

constructor (x as mammal) _internalmammalobject = new mammal _internalmammalobject.genus = x.genus _internalmammalobject.family = x.family //stuff end sub