xHarbour Reference Documentation > Statement Reference xHarbour Developers Network  

OVERRIDE METHOD

Replaces a method in an existing class.

Syntax

OVERRIDE METHOD <MethodName> ;
       IN CLASS <ClassName> WITH <FunctionName>

Arguments

<MethodName>
This is the symbolic name of the method to replace.
IN CLASS <ClassName>
This is the symbolic name of the class in which the method is replaced.
WITH <FunctionName>
This is the symbolic name of the function implementing the code for the replaced method.

Description

The OVERRIDE METHOD statement replaces the implementation of a method in an existing class. When an object receives the message <MethodName>, the code implemented in <FunctionName> is executed and the previous implementation is no longer accessible. <FunctionName> is usually implemented as a STATIC FUNCTION where the self object is retrieved with HB_QSelf().

OVERRIDE METHOD can be used to replace the method implementation in a higher level of a class tree. This is especially useful when the source code of the classes is not available. However, great care must be taken in order not to break existing class inheritance.

An alternative is the declaration of a sub-class using the FROM option in the class declaration and implement the new method in the sub-class.

Info

See also:ASSOCIATE CLASS, DELEGATE, EXTEND CLASS...WITH METHOD, METHOD (implementation), OPERATOR
Category: Class declaration , Declaration , xHarbour extensions
Header:hbclass.ch
Source:vm\classes.c
LIB:xhb.lib
DLL:xhbdll.dll

Example

// The example outlines how a method can be replaced in existing
// classes and how the self object is retrieved in the implementation
// of a replacement method.

   #include "hbclass.ch"

   PROCEDURE Main()
      LOCAL cVar := "Hello", GetList := {}

      CLS

      OVERRIDE METHOD Display IN CLASS GET WITH MyGetDisplay
      OVERRIDE METHOD AsString IN CLASS ARRAY WITH MyArrayString

      @ 10,10 GET cVar

      ? GetList:AsString()
   RETURN

   STATIC FUNCTION MyGetDisplay
      LOCAl Self := HB_QSelf()
      Alert( ::VarGet() )
   RETURN Self

   STATIC FUNCTION MyArrayString()
      LOCAl Self := HB_QSelf()
   RETURN "{ ... }"

Copyright © 2006-2007 xHarbour.com Inc. All rights reserved.
http://www.xHarbour.com
Created by docmaker.exe