ONLINE HELP
 WINDEVWEBDEV AND WINDEV MOBILE

This content has been translated automatically.  Click here  to view the French version.
Help / WLanguage / OOP (Object Oriented Programming)
  • Overview
  • Syntaxes
  • Class inheritance
  • Detailed syntax
  • Syntax kept for backward compatibility
  • Redefining methods
  • Example
  • Example
  • Syntax used to call a method of base class
  • Syntax 1
  • Syntax 2
  • Syntax 3
  • Remark
WINDEV
WindowsLinuxJavaReports and QueriesUser code (UMC)
WEBDEV
WindowsLinuxPHPWEBDEV - Browser code
WINDEV Mobile
AndroidAndroid Widget iPhone/iPadIOS WidgetApple WatchMac Catalyst
Others
Stored procedures
Overview
The hierarchical organization in class and sub-class has allowed to create the notion of inheritance.
In other words, an object of the sub-class A (derived class) that has the same characteristics as the class B (ancestor class) as well as its own characteristics inherits from all the characteristics of class B without having to duplicate the programs in the object of the sub-class A. The number of lines of code is reduced.
The inheritance is the mechanism by which the class currently described uses the methods and the members defined in the existing classes.
  • The existing class is called Ancestor class or Base class.
  • The new class is called Derived class. The derived class includes the ancestor class and adds new methods, new members and new properties to it.
The purpose of the inheritance is to retrieve, for a class, the methods developed for another class, by adding the specific features of the new class.
The objects found in a derived class can access all methods, all members and all properties of ancestor classes ; it is as if the methods, the members and the properties of ancestor classes were part of the derived class.
Characteristics of inheritance:
  • An inheritance can be multiple. In this case, the derived class can derive from several ancestor classes.
  • An inheritance can be private or public (by default).
    If the inheritance is public, you have the ability to access the methods, the properties and the members inherited from the outside of the class.
    If the inheritance is private, only the methods of the derived class can access the inherited methods, properties and members.
  • Like in all languages, the polymorphism operates with the dynamic objects only.
Syntaxes

Class inheritance

<NomClasseDérivée> est une classe
[PRIVE, PROTEGE, PUBLIC]
Hérite de <NomClasseAncêtre>
<Nom Membre classe Dérivée> est <Type du membre>
...
FIN

Detailed syntax

<NameDerivedClass>
Name identifying the derived class that is currently declared.

PRIVATE, PROTECTED, PUBLIC
Optional keyword. Indicates whether the inheritance is private or not. If this keyword is not specified, the inheritance is public.

<NameAncestorClass>
Name of the ancestor class.

<Member Name Derived Class>
Name of the member of the derived class. This member can only be used in an object of the derived class.

<Member Type>
Type of the member, chosen among the available types.
NomFichier is Class 
	Nom is string
	Extension is string
	Répertoire is string
END
 
InformationsFichier is Class 
	inherits from NomFichier
	TailleFichier is int
	DateFichier is string 
	HeureFichier is string 
END

Syntax kept for backward compatibility

<NomClasseDérivée> est une classe
[PRIVE, PROTEGE, PUBLIC]
Un objet <NomClasseAncêtre>
<Nom Membre classe Dérivée> est <Type du membre>
...
FIN
Redefining methods
In a derived class, a method of the base class can be redefined by creating in the derived class a method with the same name as the one of the base class.
The redefinition of methods is used to modify the behavior of the method defined in the base class ; the derived class can eventually redefine the method according to its requirements.
The redefined method is a virtual method by default.
Remark: method overloading (use of two methods of the same name in the same class) is handled in WLanguage. For more details, see Prototype overload/Overload.
Note: the Object keyword is used to access the current object within a method.

Example

The FileInfo class inherits from the FileName class. These two classes have a method named Display. The Display method of the FileName class is used ty the Display method of the FileInfo class.
FileName classFileInfo class
Declaration of the classDeclaration of the class
NomFichier is Class
	Nom is string
 	Extension is string
 	Répertoire is string 
END
InformationsFichier is Class
	inherits from NomFichier
	TailleFichier is int
	DateFichier is string  
	HeureFichier is string  
END
Display methodDisplay method
PROCEDURE Affiche()
Trace(:Nom) 
Trace(:Extension) 
Trace(:Répertoire)
PROCEDURE AFFICHE() 
Ancestor:Affiche() 
Trace(:TailleFichier) 
Trace(:DateFichier) 
Trace(:HeureFichier)

Example

The polymorphism operates with the dynamic objects only.
In this example, a virtual animal class is created. Two classes (rabbit and cat) inherit from this class. We want to create a list of different objects (rabbits and cats).
Animal is Class 
END
 
Chat is Class 
	inherits from Animal
END
Lapin is Class 
	inherits from Animal
END

ListeA is array of 16 animal dynamique
ListeA[1] = new chat
ListeA[1] = new lapin
Syntax used to call a method of base class

Syntax 1

Ancêtre:<Nom de la Méthode>()
Details of syntax
Ancestor
keyword used to indicate that the method to use is the method of the ancestor class immediately found before (can be used for a single inheritance). To call a method of a base class regardless of its level in the order of inheritance, syntax #2 must be used..

<Method name>
Name of method to use in the ancestor class.

Syntax 2

<Classe de base>:<Nom de la Méthode>()
Details of syntax
<Base class>
Name of ancestor class where the method to use was defined.

<Method name>
Name of method to use in the ancestor class.

Syntax 3

:<Classe de base>:<Nom de la Méthode>()
Details of syntax
<Base class>
Name of ancestor class where the method to use was defined.

<Method name>
Name of method to use in the ancestor class.

Remark

For the constructors and destructors, we recommend that you use specific syntaxes (see Syntax of constructors for the base classes and members).
Related Examples:
WD Simple OOP Training (WINDEV): WD Simple OOP
[ + ] The "WD Simple OOP" example is an educational example about the OOP with WINDEV. This example presents the operating mode of:
- classes,
- inheritances,
- virtual procedures,
- UML diagrams,
- ...
WD Controlling word processor Training (WINDEV): WD Controlling word processor
[ + ] The purpose of this example is to propose a universal interface, allowing to control these applications without having to worry about the application to control. This example is based on an object-oriented programming concept: the polymorphism. It consists in using different objects (OpenOffice object and Microsoft Word object) without really knowing which one is used.
Minimum version required
  • Version 9
This page is also available for…
Comments
Click [Add] to post a comment

Last update: 09/24/2024

Send a report | Local help