Announcement

Collapse
No announcement yet.

Possible to inherit implemenation of the same interface in a new class?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Possible to inherit implemenation of the same interface in a new class?

    Hi there,

    Let's hope I can make myself clear:

    I have a single interface that I want to implement in different classes. The implementations of the interface in the classes are almost the same, only a few of the METHODs differ.

    What I would like to do is the following:

    Code:
    #COMPILE EXE
    #DIM ALL
    
    'I usually "publish" Interfaces that I can use outside the current file in the program's header.
    '(this way I know which interfaces are supposed to be implemented by the
    'program and which interfaces I only created for internal use)
    INTERFACE IncredibleInterface
        INHERIT CUSTOM
        METHOD AAA()
        METHOD BBB()
        METHOD CCC()
    END INTERFACE
    
    
    'Here I create class CTest1 that has the "default implementation" of above's IncredibleInterface interface.
    CLASS CTest1
        INTERFACE IncredibleInterface
            INHERIT CUSTOM
    
            METHOD AAA()
                ? "AAA1"
            END METHOD
    
            METHOD BBB()
                ? "BBB1"
            END METHOD
    
            METHOD CCC()
                ? "CCC1"
            END METHOD
        END INTERFACE
    END CLASS
    
    
    'Now I want to create class CTest2, which implements the IncredibleInterface just like CTest1.
    'I want to override 1 of the methods though, this is the only Method which has its own implementation.
    CLASS CTest2
        INTERFACE IncredibleInterface
            INHERIT CTest1,IncredibleInterface
            OVERRIDE METHOD BBB()
                ? "BBB2"
            END METHOD
        END INTERFACE
    END CLASS
    
    
    FUNCTION PBMAIN () AS LONG
    
        DIM CT1 AS IncredibleInterface
        DIM CT2 AS IncredibleInterface
        CT1 = CLASS "CTest1"
        CT1.AAA
        CT1.BBB
        CT1.CCC
    
        CT2 = CLASS "CTest2"
        CT2.AAA
        CT2.BBB
        CT2.CCC
        
        CT1 = NOTHING
        CT2 = NOTHING
        
        WAITKEY$
    END FUNCTION
    When I try this in PB/CC 5.01, I get the following error though:

    PowerBASIC Console Compiler
    PB/CC Version 5.01
    Copyright (c) 1998-2009 PowerBasic Inc.
    Venice, Florida USA
    All Rights Reserved

    Error 816 in C:\VDLTech\Berra\Tests\InheritTest2.bas(10:005): Internal system error
    Line 10: END INTERFACE
    Do I understand correctly that inheritance of an implementation is only possible when we create a new interface? That's not what I want (in this case).

    The only solution I came up with is the following, but it's not really pretty IMHO, not to mention too much typing :

    Code:
    #COMPILE EXE
    #DIM ALL
    
    'I usually publish Interfaces that I use outside this file outside the class, like this:
    INTERFACE IncredibleInterface
        INHERIT CUSTOM
        METHOD AAA()
        METHOD BBB()
        METHOD CCC()
    END INTERFACE
    
    
    'Here I create class CTest1 that has the "default implementation" of above's IncredibleInterface interface.
    CLASS CTest1
        INTERFACE IncredibleInterface
            INHERIT CUSTOM
    
            METHOD AAA()
                ? "AAA1"
            END METHOD
    
            METHOD BBB()
                ? "BBB1"
            END METHOD
    
            METHOD CCC()
                ? "CCC1"
            END METHOD
        END INTERFACE
    END CLASS
    
    
    'Nowe I want to create class CTest2, which implements the IncredibleInterface just like CTest1.
    'I want to override 1 of the methods though, this is the only Method which its own implementation.
    CLASS CTest2
        INSTANCE m_ct1 AS IncredibleInterface
    
        CLASS METHOD Create
            m_ct1 = CLASS "CTest1"
        END METHOD
    
        CLASS METHOD Destroy
            'Yes, I know it's not neccessary, but I like to clean up my mess.
            m_ct1 = NOTHING
        END METHOD
    
        INTERFACE IncredibleInterface
            INHERIT CUSTOM
            METHOD AAA()
                m_ct1.AAA
            END METHOD
    
            METHOD BBB()
                ? "BBB2"
            END METHOD
    
            METHOD CCC()
                m_ct1.CCC
            END METHOD
    
        END INTERFACE
    END CLASS
    
    
    FUNCTION PBMAIN () AS LONG
    
        DIM CT1 AS IncredibleInterface
        DIM CT2 AS IncredibleInterface
        CT1 = CLASS "CTest1"
        CT1.AAA
        CT1.BBB
        CT1.CCC
    
        CT2 = CLASS "CTest2"
        CT2.AAA
        CT2.BBB
        CT2.CCC
    
        CT1 = NOTHING
        CT2 = NOTHING
    
        WAITKEY$
    END FUNCTION
    Does anyone have any suggestions or know of a different solution that I missed?

    Best regards, Vincent

  • #2
    You can, but changing the name of the second interface.

    Code:
    'SED_PBCC
    #COMPILE EXE
    #DIM ALL
    
    'I usually "publish" Interfaces that I can use outside the current file in the program's header.
    '(this way I know which interfaces are supposed to be implemented by the
    'program and which interfaces I only created for internal use)
    INTERFACE IncredibleInterface
        INHERIT CUSTOM
        METHOD AAA()
        METHOD BBB()
        METHOD CCC()
    END INTERFACE
    
    
    'Here I create class CTest1 that has the "default implementation" of above's IncredibleInterface interface.
    CLASS CTest1
        INTERFACE IncredibleInterface
            INHERIT CUSTOM
    
            METHOD AAA()
                ? "AAA1"
            END METHOD
    
            METHOD BBB()
                ? "BBB1"
            END METHOD
    
            METHOD CCC()
                ? "CCC1"
            END METHOD
        END INTERFACE
    END CLASS
    
    
    'Now I want to create class CTest2, which implements the IncredibleInterface just like CTest1.
    'I want to override 1 of the methods though, this is the only Method which has its own implementation.
    CLASS CTest2
        INTERFACE IncredibleInterface2
            INHERIT CTest1,IncredibleInterface
            OVERRIDE METHOD BBB()
                ? "BBB2"
            END METHOD
        END INTERFACE
    END CLASS
    
    
    FUNCTION PBMAIN () AS LONG
    
        DIM CT1 AS IncredibleInterface
        DIM CT2 AS IncredibleInterface2
        CT1 = CLASS "CTest1"
        CT1.AAA
        CT1.BBB
        CT1.CCC
    
        CT2 = CLASS "CTest2"
        CT2.AAA
        CT2.BBB
        CT2.CCC
        
        CT1 = NOTHING
        CT2 = NOTHING
        
        WAITKEY$
    END FUNCTION
    Forum: http://www.jose.it-berater.org/smfforum/index.php

    Comment


    • #3
      Error 816 in C:\VDLTech\Berra\Tests\InheritTest2.bas(10:005): Internal system error
      Line 10: END INTERFACE
      Slightly O/T, but that is an automatic "report to PB Support."
      Michael Mattias
      Tal Systems (retired)
      Port Washington WI USA
      [email protected]
      http://www.talsystems.com

      Comment


      • #4
        The issue here is that a child interface can inherit from a parent interface. A child interface cannot inherit from itself. As noted previously, the derived interface must be unique and therefore have a different name.

        The next update to PowerBASIC will generate a more descriptive exception report in this case.

        Best regards,

        Bob Zale
        PowerBASIC Inc.

        Comment

        Working...
        X