Abstraction

BlitzMax Forums/BlitzMax Programming/Abstraction

_Skully(Posted 2009) [#1]
I'm misunderstanding something with Abstraction?

I have:

Type TMSequencer

	Field _Active:Int=True
	Method Active:Int()
		Return _Active
	End Method

	Method ParamUpdate(param:Int, value:Float)
		' assignment
	End Method
End Type

Type TMObj Abstract
	Method ParamUpdate(Param:Int,value:Float) Abstract
	Method Active:Int() Abstract
End Type

obj:TMSequencer=New TMSequencer

If obj
  T:TMObj=TMObj(obj)                    <- casting problem?
	If T.Active()
 		' do...
	EndIf
EndIf




Am I missing something?


plash(Posted 2009) [#2]
I'm misunderstanding something with Abstraction?
Yes..

I'm not entirely sure what you're going for here, but I'll take a whack at it:



Czar Flavius(Posted 2009) [#3]
You forgot to extend one type to the other.


Jesse(Posted 2009) [#4]
no need for casting this will work just as well and is probably more efficient:
Type TMObj Abstract
	Method ParamUpdate(Param:Int,value:Float) Abstract
	Method Active:Int() Abstract
End Type

Type TMSequencer Extends TMobj

	Field _Active:Int=True
	Method Active:Int()
		Return _Active
	End Method

	Method ParamUpdate(param:Int, value:Float)
		' assignment
	End Method
End Type


obj:TMSequencer=New TMSequencer

If obj
  T:TMObj = obj                  '  <- casting problem?
	If T.Active()
 		' do...
	EndIf
EndIf

careful when creating 'abstract types' as all of the extended types must have at least the same methods and functions created in the 'abstract type' with the same number of parameters and parameter types.


Kurator(Posted 2009) [#5]
My suggestion:

Use the abstract Class for the Variable, so you can use any Implementations for your If-Statement

SuperStrict

Type TMObj Abstract
	Method ParamUpdate:Int(Param:Int,value:Float) Abstract
	Method Active:Int() Abstract
End Type

Type TMSequencer Extends TMObj
	Field _Active:Int=True
	Method Active:Int()
		Return _Active
	End Method

	Method ParamUpdate(param:Int, value:Float)
		' assignment
	End Method
End Type

Local obj:TMObj = New TMSequencer

If obj.Active()
 		' do...
EndIf



_Skully(Posted 2009) [#6]
I think i'm missing the point of Abstraction then... because if I extend any type the base methods, fields, and functions are available to the extended type... no need to abstract that correct?

My impression of Abstraction was to be able to access a specific set of methods associated with any particular object by casting it to the abstract class... provided the object being abstracted has the same methods (with the same signature) as the abstract class. Kind of like a gateway.

The sequencers need to access two methods of any object that is attached to it.. They are used for the menu system and the particle system...


GW(Posted 2009) [#7]
Abstract just means that you cant create an instance of that type. It can only be the parent of another type. And the child must define the methods as well.
If you not making an api to expose to other developers, there is no real reason to ever use abstract.


Czar Flavius(Posted 2009) [#8]
It's just a nicity, like painting a car. If you don't care about what colour your car is, just ignore Abstract.

Here's the textbook example of what Abstract is for:
You have an Abstract TShape type with a Draw method, and then you extend this to TSquare, TCircle, etc... As TShape itself can't be drawn, it doesn't make sense for you to create an actual TShape, but you could create say an array of type TShape which could then store a mixture of TSquare, TCircle and anything else you define. You can then loop through them with a local TShape variable, and tell them to Draw, using their respective methods.

There is nothing to stop you from not using Abstract, and just giving TShape some blank (empty) methods, if you feel better with that.


_Skully(Posted 2009) [#9]
OOOOoooooohhhhh K!

Now I see what I need to do to make this work. Thanks everybody!


Dreamora(Posted 2010) [#10]
I know that text book ... head first books are great ;)