Inheritance Sanity Check

BlitzMax Forums/BlitzMax Beginners Area/Inheritance Sanity Check

Gabriel(Posted 2006) [#1]
Hi,

I thought I had this right, and I wrote a little example which confirmed it, but I'd still like a little extra confirmation in case there are deeper issues involved here.

If I have a type (b) which extends another type (a) and they both have the same function or method. If, in my program, I have an object which was created as a type b, but in order to achieve polymorphism, I have it as a type a, and then call that method, am I correct in thinking that it is actually the method of b which will be executed?

I am ( for the sake of polymorphism ) treating it as a type a, but it is in fact, a type b, so that's the method that gets called, right? And only that method?

My example I used to verify :



Type A
	
	Function PrintSomething()
		Print "DO NOT WANT TO PRINT ONLY THIS"
	End Function
	
End Type


Type B Extends A
	
	Function PrintSomething()
		Super.PrintSomething()
		Print "I WANT TO PRINT THIS TOO"
	End Function
	
End Type


Function CreateThing:A()
	Return New B
End Function


Global Thing:A=CreateThing()

Thing.PrintSomething()


It executes b's function, even though I have it cast as a type a. If I remove the call to Super, type a's function is never called. So I think that confirms what I believed to be the case, right? No deeper issues here?


H&K(Posted 2006) [#2]
Yep you got it.

When you say Thing is a Type A. And then passit a type B, thats ok because B has a "IS A" relationship with A.

That is anywhere that wants and A will accept a B, because it is a A.
In your example Thing knows its a b, and so calls the overridden function.

http://www.blitzbasic.com/Community/posts.php?topic=61748#691599


Defoc8(Posted 2006) [#3]
if it didnt work, it would be pointless..this is how it was
intended to be used. A collection of base refs makes
managing specific objects easier..

type shape abstract
method draw() abstract
endtype

type triangle extends shape
method draw()
....
....
end method
endtype

type square extends shape
method draw()
....
....
end method
endtype

myshape.shape=new triangle

myshape.draw() ;draw the shape..whatever it might be

sorry for the tarded example...dont mean to be
patronising..im rusty + quite stupid anyway ;)

...er..hmmm.. :]

cheers!


Azathoth(Posted 2006) [#4]
I thought functions existed in the type not the instance?


Paposo(Posted 2006) [#5]
Hello.

Try:
myshape.shape.draw()

Bye,
Ramon


Dreamora(Posted 2006) [#6]
Azathoth: thats right. But you can call them from the instance as well, you just need to keep in mind, that you won't be able to operate on the instance data then as a function has no self and does not know where it was called from.


Gabriel(Posted 2006) [#7]
Thanks everyone.

Def: You're right. I have tended to almost exclusively have types which only have abstract methods and functions in the base type though. For whatever reason I've never had implemented methods and functions in the base type, so I've never had them to consider. Of course, if it did otherwise, it would error trying to call an abstract function or method, but it still seems weird because I haven't done it like this before. You're right though, now that I think about it, it would be pretty useless if it worked any other way.