OOP: Polymorphism for beginners

BlitzMax Forums/BlitzMax Tutorials/OOP: Polymorphism for beginners

Kurator(Posted 2008) [#1]
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"

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:



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:


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.


tonyg(Posted 2008) [#2]
Are you sure this is polymorphism rather than Inheritence?
Isn't Polymorpism where you declare the instance variable with the basetype but initialise it with the extended type. That way you can use the basetype but have it 'morphed' to use the extended types method :
e.g.



Kurator(Posted 2008) [#3]
Polymorphism is used in Type Pictutre - there you have a List (like in your example) - in this List you can put every derived class of GraphicalObject. Within the draw mehtod it is morphed to the correct draw method of the extended type :)


tonyg(Posted 2008) [#4]
Hmmm, OK. Don't want to be negative but it seems a confusing example for beginners tutorial.
Having said that, I don't class myself as a beginner so maybe I am not the right person to comment.


Kurator(Posted 2008) [#5]
Ok, thank you for your feedback - I'll think about a rework and some additions :)


Kurator(Posted 2008) [#6]
A very small update, with some additonal graphics


Jesse(Posted 2008) [#7]
I had bmax for about 3 to 4 years, and even though I have learned most oop from these here forums, I can say I am still learnig the basics. this have been helpfull still. Thank You for the tutorial and taking the time to draw and include images. You are appreciated!


Kurator(Posted 2008) [#8]
Sorry, the last picture had a wrong URI, I corrected it :)


Compt-Man(Posted 2010) [#9]
Thanks All ways wondered how to do this!













----------------
http://arlingtonheatingandcooling.com/


psychoullis(Posted 2010) [#10]
Very nice tutorial. Helped me to get started again. It would be nice to see more tutorials from you man... But its been a year :D