Abstract Methods... pop quiz

BlitzMax Forums/BlitzMax Programming/Abstract Methods... pop quiz

Brucey(Posted 2008) [#1]
Okay... what's wrong with this, exactly ?
SuperStrict

Type SuperBlah

	Method New()
		DoIt()
	End Method
	
	Method DoIt() Abstract
	
End Type

Type SubBlah Extends SuperBlah

	Method DoIt()
	End Method

End Type

New SubBlah


...answers on a postcard to...

:-/


Htbaa(Posted 2008) [#2]
You're calling an abstract method in the constructor of an abstract type (though not declared as abstract, it's abstract because of the abstract method).


Brucey(Posted 2008) [#3]
So, it's too early to call it?

I guess that makes some kind of sense.

Thanks :-)


JoshK(Posted 2008) [#4]
When you call a method in the New() method, the class' own method is called, not the one that would normally be used.


Warpy(Posted 2008) [#5]
While what you said makes sense, that really doesn't sound good at all.

If you change the New() method to a different one and call it *after* the object is created, it knows what to do:

SuperStrict

Type SuperBlah

	Method DoThat()
		DoIt()
	End Method
	
	Method DoIt() Abstract
	
End Type

Type SubBlah Extends SuperBlah

	Method DoIt()
	End Method

End Type

Local a:SubBlah=New SubBlah

a.DoThat		'Works, but OK, it's a SubBlah now, it knows what it is.

SuperBlah(a).DoThat		'So it should think it's dealing with a SuperBlah, right? Wrong! This works too!



So is there a really good reason why it can't do this in the New() method? Is it to save the compiler time rewriting function references for each extending type?


Brucey(Posted 2008) [#6]
So is there a really good reason why it can't do this in the New() method?

Would be nice, and would certainly make my design cleaner...


Gabriel(Posted 2008) [#7]
So is there a really good reason why it can't do this in the New() method?

Surely the reason is because this is how inheritance works. When you create an object, it becomes a base class object first, then becomes an extended class object, and so on if there are multiple levels of inheritance. So in the New method of the base object, it cannot call the inherited method, because at this point in time, it is not of an inherited type. None of the inherited properties (fields and methods) have been added yet.

And with delete, the same is true. The delete methods are called in reverse order, and so by the time it gets back to the base class, it is no longer inherited. Those inherited properties have been destroyed.

EDIT: Furthermore, surely it *has* to work this way or properties in the base class would override properties in the extended class if they were added in a different order.


Warpy(Posted 2008) [#8]
That is massively interesting and answers my question entirely, thank you!

So Brucey, I suppose the answer to your problem is to put whatever was in DoIt() inside the New() method itself for each extended type, or at least a call to DoIt() each time.


Brucey(Posted 2008) [#9]
suppose the answer to your problem is to put whatever was in DoIt() inside the New() method itself for each extended type

Aye... that's what I decided to do - for now. Not too bad as there is only one subclass at the moment.. but ... my design!!!! *sniff*

Oh well :-)

Thanks to all for the insights... interesting reading as always.


Grey Alien(Posted 2008) [#10]
ultra geeky reading, but I like it.