What is inheritance?

Inheritance is all about code reuse. You can reuse the definitions of an interface, or you can reuse complete sections of code.

INTERFACE INHERITANCE is defined by COM standards, and available for use by any COM object. This form of inheritance applies only to the definition of each item in an interface, rather than the underlying code. Interface inheritance gives you the option to use one interface in multiple classes (objects). Because the interface definition remains identical in each instance, you can often use the identical (or similar) code to manipulate different objects. With this form of inheritance, the programmer must provide appropriate code for each of the Methods and Properties in every implementation of the interface.

IMPLEMENTATION INHERITANCE is the process whereby a CLASS derives all of the functionality of an interface implemented elsewhere. That is, the derived class now has all the methods and properties of this new, extended version of a Base Class! This form of inheritance is offered by PowerBASIC, even though it is not required by the COM Specification.

You can extend the functionality of an interface you created earlier by adding new methods and properties to the derived interface/class. The syntax for adding extra methods (not in the Base Class) is the same as adding methods to a standard class -- just add methods and properties, as always.

You can add to, or replace, the functionality of a particular method or property by coding a replacement which is preceded by the word OVERRIDE. The overriding method must have the same name and signature (parameters, return value, etc.) as the one it replaces. When you implement a new method in a derived class, you may call a method in the Base Class by using the pseudo-object MYBASE. This allows you to extend the original functionality, or replace it entirely.

Inheritance is implemented by use of the INHERIT statement within an INTERFACE / END INTERFACE block. The word INHERIT is followed by the class name and interface name of the code to be inherited. Both are necessary, because COM allows you to have multiple implementations of any particular interface.

CLASS MyClass

INTERFACE MyFace

INHERIT IDISPATCH

METHOD aaa()

' code...

END METHOD

METHOD bbb()

' code...

END METHOD

METHOD ccc()

' code...

END METHOD

METHOD ddd()

' code...

END METHOD

END INTERFACE

END CLASS

CLASS TheClass

INTERFACE TheFace

INHERIT MyClass, MyFace

OVERRIDE METHOD bbb()

' new code

END METHOD

OVERRIDE METHOD ddd()

' new code

END METHOD

METHOD xxx()

END METHOD

END INTERFACE

END CLASS

Note that the derived interface "TheFace" first inherits IDISPATCH, and then, all four methods from "MyFace" (aaa,bbb,ccc,ddd). However, because of the OVERRIDE statements, both bbb() and ddd() are replaced by newer versions of these methods. Note that a derived class may be inherited by yet another class, repetitively. The depth of this inheritance is limited only by available memory.

The pseudo-object MYBASE may be used within a derived class to access a method in the original base class. For example, if you placed:

MyBase.bbb()

in the above derived code, it would execute the method bbb() in the parent interface/class. You could then use the results to extend or modify actions in your newer code.

When you inherit an interface, the inherited constructor and destructor methods (CREATE and DESTROY) are disabled, in case you wish to change their functionality in the derived interface. If you wish to execute them as-is, you can simply add MYBASE.CREATE and/or MYBASE.DESTROY in the derived CREATE/DESTROY methods.

See Also

What is an object, anyway?

What does an Interface look like?

Just what is COM?

How do you create an object?