Question about inheritance

BlitzMax Forums/BlitzMax Programming/Question about inheritance

PowerPC603(Posted 2004) [#1]
I've got this code (I was testing some inheritance-stuff):

Type Sector
	Method New()
		Print "Sector method was called"
	End method

	Method Create()
		Print "New sector has been created"
	End Method
End type

Type Station Extends Sector
	Method New()
		Print "Station method was called"
	End method

	Method Create()
		Print "New station has been created"
	End Method
End type

NewStation:Station = New Station
NewStation.Create()


The first line after the type-declarations (NewStation:Station = New Station) executed both "New()"-methods (both strings in the New-method are printed), but the second line (NewStation.Create())
only executed the Create()-method from the Station type.

Is this normal?
This inheritance-stuff is confusing me a bit.

And using this setup, how could I execute the Create-method from the Sector type (without renaming both methods)?
The easiest way would be to rename both Create()-methods to CreateSector and CreateStation, as I tried and it worked as expected.


Dreamora(Posted 2004) [#2]
Yes this is normal ...
But in a real "create" you would have a new command as well so the problem does not exist anymore :)


Curtastic(Posted 2004) [#3]
how can you have a new command in the create() function?
can you show code for that?


Ole JR(Posted 2004) [#4]
I think this is what he's talking about.
Type Station Extends Sector
        
	Function Station:Create() 'NOTE Changed to Function
                Print "New Station created"
                Return New Station 'Returns a New Station
        End Function

End type

NewStation:Station = Station.Create()



Dreamora(Posted 2004) [#5]

Type test

  method new()
   Print "trara"
  endmethod

  function create:test()
    ' another variant of type creation
    print "trara2"
    return new test
  endfunction

  method create()
    ' this function can NOT create types / object!
    print "some operation on an existing type instance"
  endfunction
endtype



methods can not be used to create types, they can only be applied to existing type instances.

so if you want to have an own creation function you need to use function with a return of the given type and to return that instance you will need to create it which needs "new".
So using a creation function makes only sense if you want to have a creation feature with input values, otherwise it is not needed as "default settings" can be done a overwriten "method new()" as you already did.

Your creation won't work, if you call "create" to create a station or sector you will get an error that there is no such identifier ( as type instance not created )


And your method "create" in station overwrites the original create. if you want to call the original one as well, you will need to use super.create


Curtastic(Posted 2004) [#6]
okay thanks


Ole JR(Posted 2004) [#7]
OK got it mixed up a bit..
Should be, as you said:
 function create:station()

I my self is trying to get this OO thing under the belt.. :-/


PowerPC603(Posted 2004) [#8]
The method-name "Create()" was chosen wrongly, I think.

It would be better named if it was "PrintSomething".

The question remains the same:
How come that both "New()"-methods are called and by calling "NewStation.PrintSomething()" it only calls the "PrintSomething()"-method of the derived type?

To call the PrintSomething() method inside the base type (Sector), how could I do this?
Type Sector
	Method New()
		Print "Sector method was called"
	End method

	Method PrintSomething()
		Print "New sector has been created"
	End Method
End type

Type Station Extends Sector
	Method New()
		Print "Station method was called"
	End method

	Method PrintSomething()
		Print "New station has been created"
	End Method
End type

NewStation:Station = New Station
NewStation.PrintSomething() ' <--- Calls only the Station's "PrintSomething" method

' How to call the Sector's PrintSomething method from here?



FlameDuck(Posted 2004) [#9]
How come that both "New()"-methods are called and by calling "NewStation.PrintSomething()" it only calls the "PrintSomething()"-method of the derived type?
Because the New() method is being called as part of the objects constructor at creation time, and is invoked all the way through the object creation hierarchy. Create() isn't.