Inheritance of Classes
Example:
class myclass inherit from index {
logical mflag
long _value
_find(object obj);
.
.
.
}
- A class that inherits from another class is called the child class of that
class. The inherited class is called the parent class of the child class.
- When defining a class, you can inherit the properties and methods from any
class by using the "inherit from <name>" on the class definition
line.
- All properties and methods are inherited and are visible to the child class.
- No properties or methods of the parent class can be changed in the child
class.
- Inherited properties are included with an underscore ("_") added
to the front of the name of the property, unless an underscore is already
on the front of the variable name.
- You can have nested inheritance to any number of levels and each time an
underscore is put on the front of the variable if one is not there already.
This means that you would know by looking at a property that it was inherited
but not from what level.
- A property can have the same name from one level of inheritance to another
but an error will occur if you do this to more than 1 level. A variable with
2 underscores on the front of the variable will not be created.
- Once all inhertied properites are added to a child class, they will be kept
up to date if the parent class is changed, automatically.
- Any changes made to a property that starts with an underscore will not be
kept.
- You must change the properties where they were originally defined to change
any aspect of a property.
- If a method in the child class is given the same name as a method in the
parent class, then any reference to the method without the underscore will
always point to the child's method.
- If the child class doesn't have a method with the same name as the parent
class then a reference to the method without the underscore will call the
parent class method which has the underscore in front of it.
- Any reference to a method that explicitly calls a method with the underscore
on the front will always call the inherited method or produce an error if
no method exists.
Multi-Level Inheritance:
class a {
long z
display(){
? z
}
}
class b inherit from a{
long y
long _z
_display()
}
class c inherit from b{
long x
long _y
long _z
display(){
? x,_y
=_display()
}
_display()
}
- The above example shows how the display function of class "a"
got inherited into class "c".
- The display function of class "c" displays the "x" variable
defined in class "c" and the "_y" variable defined in
class "b".
- The "_z" variable defined in class "a" is displayed
using the display function defined in class "a".
- A cross reference is kept of all references to properties or methods of
a class so that when changes are made to a class, all child classes are automatically
changed.
- Even though the display method of class "a" is shown in both class
"b" and class "c", the source code and the compiled code
for this method are only stored once in class "a". This means that
the _display method of class "b" is not compiled when class "b"
is compiled.
Note: All classes are defined within another class. This is NOT the same as inheritance. Classes are defined in classes to organize the definition of classes and keep the variable names 11 characters or less.