- 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
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 code lines 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.
Class inheritance
<NameDerivedClass> is class [PRIVATE, PROTECTED, PUBLIC] Inherits from <NameAncestorClass> <Name Member Derived Class> is <Member Type> ... END
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.
FileName is Class Name is string Extension is string Directory is string END FileInformation is Class inherits from FileName FileSize is int FileDate is string FileTime is string END
Syntax kept for backward compatibility
<NameDerivedClass> is class [PRIVATE, PROTECTED, PUBLIC] An object <NameAncestorClass> <Name Member Derived Class> is <Member Type> ... END
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: overloading a method (using two methods with the same name in the same class) is supported in WLanguage. For more details, see Prototype overload/Overload. Remark: the Object keyword is used to access the current object inside 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 class | FileInfo class | Declaration of the class | Declaration of the class |
FileName is Class Name is string Extension is string Directory is string END
|
FileInformation is Class inherits from FileName FileSize is int FileDate is string FileTime is string END
| Display method | Display method |
PROCEDURE Display() Trace(:Name) Trace(:Extension) Trace(:Directory)
|
PROCEDURE DISPLAY() Ancestor:Display() Trace(:FileSize) Trace(:FileDate) Trace(:FileTime)
|
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 Cat is Class inherits from Animal END Rabbit is Class inherits from Animal END ListA is array of 16 dynamic animals ListA[1] = new cat ListA[1] = new rabbit
Syntax used to call a method of base class Syntax 1
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
<Base Class>:<Method Name>()
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
:<Base Class>:<Method Name>()
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.
Related Examples:
|
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, - ...
|
|
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.
|
This page is also available for…
|
|
|
|