Hello out there,
this is my first tutorial, so feedback to do improve it is very welcome (even in spelling, since I am not a native speaker).
Today I want to tell you something about Polymorphism, a very important feature of an object orientated programming language - like BlitzMax.
Polymorphism means, that you can create Types which are based on ohter types - so they inherit all the fields an the methods from their base type.
But lets switch from theory to practice:
First of all - let us decide to draw something. Since we think in objects we decide to start in a very abstract way.
There is a Graphic - the graphic could have different shapes, like a circle, a box, an image, even a text is some kind of a graphic. At least a graphic can be a summary of other graphics.
But, something they all have something in common:
- They need to be drawn
So, when we want to draw something - we need some information like:
- Where do you want to draw it?
- And how has this thing to be drawn?
So we can create an very abstract Type - lets call it "GraphicalObject"
So what have we done:
- We described a Type with the name "GraphicalObject" - (our Base class.)
- This Type has two Fields (attributes) in which we can store information, simply two coordinates - x and y
- At least there is a mehtod Draw(...) - but wait, there is no code inside.
Rember - this is a base class, we still do not know what we want to draw (circle, box, text ...) - so we declare this to an Abstract Method. Do not worry to much about this - it will clarify later on.
Let's get more practical - let us draw some circles.
Again, lets have a look on the code:
- We described a Type with the name "Circle" - and with the keyword Extends we define, that Circle inherits all the information from "GraphicalObject".
- We do not need to define the coordinates for the circle, because we get those two fields by inheriting - but to describe the circle we need the information about the radius - so therefore the field r
- At least again the mehtod Draw(...) - this time with code. Since we know how to draw an Circle - we are able to fill the method with valid code.
So lets do some more easy exercises - an creat some more graphics - a box and and a text:
These two a nearly the same like the circle - but you can see - every Type has different Information to store.
Lets do a short break - and test the Types if they are working:
What happens here:
- Created instances (Objects) from the different Types
- Stored some information in the Objects
- Draw them on screen.
It seems, to work - but until now not really difficult, or?
Lets proceed one level:
In the beginning I wrote: A graphic can be a summary of ohter graphics. So we need to store some of the Objects in another Object.
Here we go:
Again a type which extends our base class - like we did before with circle, box and text.
- Objects can easily be stored in BMax Lists, so we defined a Field that is a List (and can store plenty of Elements (our defined Graphicsobjects like circels, boxes....)).
- So lets have al thought about how we have to draw this: We have some Objects in our List - these Objects in summary describe our Graphic - so our job should be to draw all the Objects which are stored in the List. The Draw(...) Mehtod does this. It loops to the whole list - and for each Object it finds it calls the Draw(..) - Method which is connected to the specific Object.
And this is where Polymorphism comes in: This only works, because all the Objects are based on the same "Master"-Type
We do not know the exact Type of each Element in the List, but we now that all the Elements are Based on the "Master"-Type "GraphicalObject". BlitzMax knows which of the three Draw(..) Methods is the correct one for each Object.
Let's look at a Graphic describing this in a sort of an UML Diagramm:

So lets have a final Test:
After doing this, we will have an Object Graph looking like this:

It is also possible to store pictures in pictures in pictures......
After doing this, we will have an Object Graph looking like this:

Thats it for the first time, hope you enjoyed it.
this is my first tutorial, so feedback to do improve it is very welcome (even in spelling, since I am not a native speaker).
Today I want to tell you something about Polymorphism, a very important feature of an object orientated programming language - like BlitzMax.
Polymorphism means, that you can create Types which are based on ohter types - so they inherit all the fields an the methods from their base type.
But lets switch from theory to practice:
First of all - let us decide to draw something. Since we think in objects we decide to start in a very abstract way.
There is a Graphic - the graphic could have different shapes, like a circle, a box, an image, even a text is some kind of a graphic. At least a graphic can be a summary of other graphics.
But, something they all have something in common:
- They need to be drawn
So, when we want to draw something - we need some information like:
- Where do you want to draw it?
- And how has this thing to be drawn?
So we can create an very abstract Type - lets call it "GraphicalObject"
Type GraphicalObject Field x:Float Field y:Float Method Draw(off_x:Float = 0, off_y:Float = 0) Abstract EndType
So what have we done:
- We described a Type with the name "GraphicalObject" - (our Base class.)
- This Type has two Fields (attributes) in which we can store information, simply two coordinates - x and y
- At least there is a mehtod Draw(...) - but wait, there is no code inside.
Rember - this is a base class, we still do not know what we want to draw (circle, box, text ...) - so we declare this to an Abstract Method. Do not worry to much about this - it will clarify later on.
Let's get more practical - let us draw some circles.
Type Circle Extends GraphicalObject Field r:Float Method Draw(off_x:Float = 0, off_y:Float = 0) DrawOval (x+off_x, y+off_y, r, r) EndMethod EndType
Again, lets have a look on the code:
- We described a Type with the name "Circle" - and with the keyword Extends we define, that Circle inherits all the information from "GraphicalObject".
- We do not need to define the coordinates for the circle, because we get those two fields by inheriting - but to describe the circle we need the information about the radius - so therefore the field r
- At least again the mehtod Draw(...) - this time with code. Since we know how to draw an Circle - we are able to fill the method with valid code.
So lets do some more easy exercises - an creat some more graphics - a box and and a text:
Type Box Extends GraphicalObject Field w:Float Field h:Float Method Draw(off_x:Float = 0, off_y:Float = 0) DrawRect(x+off_x, y+off_y, w, h) EndMethod EndType Type Text Extends GraphicalObject Field text:String Method Draw(off_x:Float = 0, off_y:Float = 0) DrawText( text, x+off_x, y+off_y) EndMethod EndType
These two a nearly the same like the circle - but you can see - every Type has different Information to store.
Lets do a short break - and test the Types if they are working:
Type GraphicalObject Field x:Float Field y:Float Method Draw(off_x:Float = 0, off_y:Float = 0) Abstract EndType Type Circle Extends GraphicalObject Field r:Float Method Draw(off_x:Float = 0, off_y:Float = 0) DrawOval (x+off_x, y+off_y, r, r) EndMethod EndType Type Box Extends GraphicalObject Field w:Float Field h:Float Method Draw(off_x:Float = 0, off_y:Float = 0) DrawRect(x+off_x, y+off_y, w, h) EndMethod EndType Type Text Extends GraphicalObject Field text:String Method Draw(off_x:Float = 0, off_y:Float = 0) DrawText( text, x+off_x, y+off_y) EndMethod EndType Graphics 640,480 HideMouse t1:Circle = New Circle t1.x = 20 t1.y = 100 t1.r = 20 t2:Circle = New Circle t2.x = 120 t2.y = 100 t2.r = 20 t3:Box = New Box t3.x = 0 t3.y = 60 t3.w = 60 t3.h = 20 t4:Box = New Box t4.x = 100 t4.y = 60 t4.w = 60 t4.h = 20 t5:Text = New Text t5.x = 00 t5.y = 20 t5.text = "This is a Test" While Not KeyHit(KEY_ESCAPE) Cls t1.Draw() t2.Draw() t3.Draw() t4.Draw() t5.Draw() Flip Wend
What happens here:
- Created instances (Objects) from the different Types
- Stored some information in the Objects
- Draw them on screen.
It seems, to work - but until now not really difficult, or?
Lets proceed one level:
In the beginning I wrote: A graphic can be a summary of ohter graphics. So we need to store some of the Objects in another Object.
Here we go:
Type Picture Extends GraphicalObject Field elements:TList = New TList Method Draw(off_x:Float = 0, off_y:Float = 0) Local element:GraphicalObject For element=EachIn elements element.draw(x+off_x, y+off_y) Next EndMethod Method Add(element:GraphicalObject) ListAddLast(elements, element) EndMethod Method Remove(element:GraphicalObject) ListRemove(elements, element) EndMethod EndType
Again a type which extends our base class - like we did before with circle, box and text.
- Objects can easily be stored in BMax Lists, so we defined a Field that is a List (and can store plenty of Elements (our defined Graphicsobjects like circels, boxes....)).
- So lets have al thought about how we have to draw this: We have some Objects in our List - these Objects in summary describe our Graphic - so our job should be to draw all the Objects which are stored in the List. The Draw(...) Mehtod does this. It loops to the whole list - and for each Object it finds it calls the Draw(..) - Method which is connected to the specific Object.
And this is where Polymorphism comes in: This only works, because all the Objects are based on the same "Master"-Type
We do not know the exact Type of each Element in the List, but we now that all the Elements are Based on the "Master"-Type "GraphicalObject". BlitzMax knows which of the three Draw(..) Methods is the correct one for each Object.
Let's look at a Graphic describing this in a sort of an UML Diagramm:

So lets have a final Test:
Type GraphicalObject Field x:Float Field y:Float Method Draw(off_x:Float = 0, off_y:Float = 0) Abstract EndType Type Circle Extends GraphicalObject Field r:Float Method Draw(off_x:Float = 0, off_y:Float = 0) DrawOval (x+off_x, y+off_y, r, r) EndMethod EndType Type Box Extends GraphicalObject Field w:Float Field h:Float Method Draw(off_x:Float = 0, off_y:Float = 0) DrawRect(x+off_x, y+off_y, w, h) EndMethod EndType Type Text Extends GraphicalObject Field text:String Method Draw(off_x:Float = 0, off_y:Float = 0) DrawText( text, x+off_x, y+off_y) EndMethod EndType Type Picture Extends GraphicalObject Field elements:TList = New TList Method Draw(off_x:Float = 0, off_y:Float = 0) Local element:GraphicalObject For element=EachIn elements element.draw(x+off_x, y+off_y) Next EndMethod Method Add(element:GraphicalObject) ListAddLast(elements, element) EndMethod Method Remove(element:GraphicalObject) ListRemove(elements, element) EndMethod EndType Graphics 640,480 HideMouse t1:Circle = New Circle t1.x = 20 t1.y = 100 t1.r = 20 t2:Circle = New Circle t2.x = 120 t2.y = 100 t2.r = 20 t3:Box = New Box t3.x = 0 t3.y = 60 t3.w = 60 t3.h = 20 t4:Box = New Box t4.x = 100 t4.y = 60 t4.w = 60 t4.h = 20 t5:Text = New Text t5.x = 00 t5.y = 20 t5.text = "This is a Test" t6:Picture = New Picture t6.x = 50 t6.y = 50 t6.Add(t1) t6.Add(t2) t6.Add(t3) t6.Add(t4) t6.Add(t5) While Not KeyHit(KEY_ESCAPE) Cls t6.x = Float(MouseX()) t6.y = Float(MouseY()) t6.Draw() Flip Wend
After doing this, we will have an Object Graph looking like this:

It is also possible to store pictures in pictures in pictures......
Type GraphicalObject Field x:Float Field y:Float Method Draw(off_x:Float = 0, off_y:Float = 0) Abstract EndType Type Circle Extends GraphicalObject Field r:Float Method Draw(off_x:Float = 0, off_y:Float = 0) DrawOval (x+off_x, y+off_y, r, r) EndMethod EndType Type Box Extends GraphicalObject Field w:Float Field h:Float Method Draw(off_x:Float = 0, off_y:Float = 0) DrawRect(x+off_x, y+off_y, w, h) EndMethod EndType Type Text Extends GraphicalObject Field text:String Method Draw(off_x:Float = 0, off_y:Float = 0) DrawText( text, x+off_x, y+off_y) EndMethod EndType Type Picture Extends GraphicalObject Field elements:TList = New TList Method Draw(off_x:Float = 0, off_y:Float = 0) Local element:GraphicalObject For element=EachIn elements element.draw(x+off_x, y+off_y) Next EndMethod Method Add(element:GraphicalObject) ListAddLast(elements, element) EndMethod Method Remove(element:GraphicalObject) ListRemove(elements, element) EndMethod EndType Graphics 640,480 HideMouse t1:Circle = New Circle t1.x = 20 t1.y = 100 t1.r = 20 t2:Circle = New Circle t2.x = 120 t2.y = 100 t2.r = 20 t3:Box = New Box t3.x = 0 t3.y = 60 t3.w = 60 t3.h = 20 t4:Box = New Box t4.x = 100 t4.y = 60 t4.w = 60 t4.h = 20 t5:Text = New Text t5.x = 00 t5.y = 20 t5.text = "This is a Test" t6:Picture = New Picture t6.x = 50 t6.y = 50 t6.Add(t1) t6.Add(t2) t6.Add(t3) t6.Add(t4) t6.Add(t5) t7:Text = New Text t7.x = 130 t7.y = -10 t7.text = "Another Test!" t8:Picture = New Picture t8.x = 10 t8.y = 10 t8.Add(t6) t8.Add(t7) While Not KeyHit(KEY_ESCAPE) Cls t8.x = Float(MouseX()) t8.y = Float(MouseY()) t8.Draw() Flip Wend
After doing this, we will have an Object Graph looking like this:

Thats it for the first time, hope you enjoyed it.