HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Inheritance problem

03-26-2010, 06:37 PM#1
Anachron
Hello. I want to have this:

Stub methods that can be overwritten.
They should be used from the thistype themself.

Collapse JASS:

struct UltraParent
    private stub method onCreate takes nothing returns nothing
    endmethod
endstruct

struct Parent extends UltraParent
    public static method create takes nothing returns thistype
        local thistype this = thistype.allocate()
        
        call .onCreate()
        
        return this
    endmethod
    //Can have the method, doesn't have to
    private method onCreate takes nothing returns nothing
        call BJDebugMsg("Doesn't work!")
    endmethod
endstruct

struct Child extends Parent
    private method onCreate takes nothing returns nothing
        call BJDebugMsg("It works!")
    endmethod
endstruct
When I create a new child
local Child c = Child.create()
it will print Doesn't work.

Is there any way to get this to work? It should print It works!
03-26-2010, 08:16 PM#2
Tot
Collapse Maybe:
struct UltraParent
    private stub method onCreate takes nothing returns nothing
    endmethod
endstruct

struct Parent extends UltraParent
    public static method create takes nothing returns thistype
        local thistype this = thistype.allocate()
        
        call .onCreate()
        
        return this
    endmethod
    //Can have the method, doesn't have to
    private stub method onCreate takes nothing returns nothing
        call BJDebugMsg("Doesn't work!")
    endmethod
endstruct

struct Child extends Parent
    private method onCreate takes nothing returns nothing
        call BJDebugMsg("It works!")
    endmethod
endstruct

03-26-2010, 08:20 PM#3
Anachron
Nope that still doesn't work.
03-27-2010, 10:41 PM#4
TheKid
What happens if you get rid of the onCreate method from Parent?

When I copy this isn't the World Editor and try to save it says that the initial stub method is private (in which case it cannot be referenced from the child struct). I don't really know how well private stub methods even work, because private means it cannot be referenced outside of the struct, and stub methods infer that they are referenced outside of the struct.
03-29-2010, 06:34 AM#5
Anachron
That doesn't work neither.
03-29-2010, 03:51 PM#6
TheKid
Try using interfaces, perhaps its an incapability of stub methods. I like using interfaces over "stub" methods because they seem more natural. Also if you extend an interface a few levels down you can still declare methods that "over-write" their parent interface-methods.
03-30-2010, 07:34 AM#7
Anachron
Am I doing something wrong?

Collapse JASS:
library ToTestStuff initializer init

    private interface eventHandler
        method onCreate takes nothing returns nothing defaults nothing
    endinterface
    
    struct parent extends eventHandler
        public static method create takes nothing returns nothing
            local thistype this = thistype.allocate()
            
            call .onCreate()
            
            return this
        endmethod
    endstruct
    
    struct child extends parent
        method onCreate takes nothing returns nothing
            call BJDebugMsg("It worked!")
        endmethod
    endstruct
    
    private function init takes nothing returns nothing
        call child.create()
    endfunction

endlibrary
03-31-2010, 11:12 PM#8
PurgeandFire111
In case you don't check THW often I guess I can post it here. You're running a non-static method from a static method so you would need to resort to some alternative:
Collapse JASS:
library ToTestStuff initializer init

    private interface eventHandler
        method onCreate takes nothing returns nothing defaults nothing
    endinterface
    
    struct parent extends eventHandler
        method asdfkjx takes nothing returns nothing
            call .onCreate()
        endmethod
        public static method create takes nothing returns nothing
            local thistype this = thistype.allocate()           
            return this
        endmethod
    endstruct
    
    struct child extends parent
        method onCreate takes nothing returns nothing
            call BJDebugMsg("It worked!")
        endmethod
    endstruct
    
    private function init takes nothing returns nothing
        call child.create().asdfkjx()
    endfunction

endlibrary

It should display "It worked!"